Routing error in Laravel 5.3 - laravel-5

I may be being thick but I am continually getting a page notfoundexception .
In a view I have the following:
<a href="{{ route('/galleryimages/{id}') }}"
This part is OK. In web.php I have:
Route::get('/galleryimages/{{id}}', function($id){
return view('gallery_pictures.pictures',['id'=>$id]);
});
The page pictures definitely exists in the subdirectory gallery_pictures.

Your route is incorrect, Laravel route parameters use one curly braces, like so: {id}. Rather than {{id}}
The helper function you are using accepts route names not route URL's, to link routes you should use the url() function
https://laravel.com/docs/5.3/helpers
https://laravel.com/docs/5.3/routing
I've provided you some for reference links if you haven't already checked them out.
HTML Fix:
My Link
Route Fix:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
});
A little extra
If you want to use the route function you must set a name for your route, you can do it as so:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
})->name('gallery_images');
Then you may access the route by doing something like so
My Link

Related

Attachment in Laravel many-to-many relation

I'm trying to do my first attachment/ detachment. I'm using two tables, user, event, and its pivot, event_user. For this one, I needed to do a button to subscribe the user to an event, so my teacher told me to use an and route it to the method subscribe. At this moment, the error that comes up is.
Route [subscribe] not defined.
Blade
<a href="{{ route('subscribe', $event->event_id) }}"
class="btn btn-primary btn-lg">Subscribe</a>
Route
Route::get('/subscribe', [SubscribeController::class, 'index']);
SubscribeController
public function index($id)
{
$user = Auth::user();
$user->events->attach($id);
return view('index');
}
I tried putting URL instead of the route in the , and it goes to /subscribe, but comes to an error that says -> Too few arguments to function 0 passed and exactly 1 expected.
I did a dd() in the component to see if the event id was the correct one, and it was. Also, apart from these errors, how can I route a method without changing the route? Can I do it using the indexController (because it's in the index where these events are located)?
First, in order to reference the route by name, you need to give it the name subscribe.
Docs: https://laravel.com/docs/master/routing#named-routes
Second, you want to add that id route parameter that you are trying to use in your controller.
Docs: https://laravel.com/docs/master/routing#required-parameters
So your route should end up looking like this:
Route::get('/subscribe/{id}', [SubscribeController::class, 'index'])
->name('subscribe');
And then you'll want to call it like this:
<a href="{{ route('subscribe', ['id' => $event->event_id]) }}"
Firstly in your blade view file you are trying to generate url using route helper like this
<a href="{{ route('subscribe', $event->event_id) }}"
class="btn btn-primary btn-lg">Subscribe</a>`
The way you call the route helper we can supposed this
You have already define a route in your web.php file which is named subscribe
and that route have paramater
But in your web.php file you have this route
Route::get('/subscribe', [SubscribeController::class, 'index']);
This route doesn't have any name.
And it doesn't expect any parameter
To fix that you should only change the way you have define you route in the web.php file like this
Route::get('/subscribe/{id}', [SubscribeController::class, 'index'])
->name('subscribe');
With this route we define:
id as a required parameter
And the route is named subscribe

Route keep asking for parameter even if not set to receive any

I'm new to laravel and I'm having trouble with one of my web routes...
I grouped some routes with the auth middleware
Route::middleware(['auth:web'])->group(function ($router) {
Route::get('/news', [NewsController::class, 'news'])->name('news');
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
Route::get('/marketplace', [MarketplaceController::class, 'marketplace'])->name('marketplace');
});
Before this issue, I have set a user_id parameter for the news route and then removed it the next day, now the news route keeps returning error saying it needs user_id.
here's my controller:
<?php
namespace App\Http\Controllers;
class NewsController extends Controller
{
public function news() {
return view('modules.news.news');
}
}
and since im using laravel-vue mix, my modules.news.news view looks like this:
#section('content')
<newsfeed inline-template>
<div>
...
...
</div>
</newsfeed>
#endsection
I've checked the web route, the controller, the blade, and I really can't see anything that requiring the news route to receive a parameter.
can someone point out where I went wrong?
Your route file might be cached, try running php artisan route:clear and see if this fixes the issue.

Laravel routes with vue/vue router

I'm basically using VueRouter to create all my routes which is working really well. However, my application is built using Laravel. So if I refresh any of the routes I get an error that the route is not defined. So at the minute in every controller I've had to add an index function. This just returns the view of my app.blade which is just the usual tags etc and the to load my single page app. I'm just wondering if there is a cleaner solution? I guess I could move the return view into a single Controller and make my Controllers extend this. But I'm just wondering if there is a better way I'm missing?
i.e. one of my routes via VueRouter:
{
path: "/clients",
name: "clients",
component: () => import(/* webpackChunkName: "clients" */ "../resources/js/components/Views/Clients/Clients.vue")
},
The route in my clients.php file
Route::get('/clients', [App\Http\Controllers\ClientController::class, 'index'])->name('clients');
Then the ClientController index function
public function index()
{
return view('app');
}
It would just be nice to have the loading of the app.blade done somewhere else and not need to be specified per controller. Any help would be appreciated to ensure it's all done efficiently!
Thanks!
Here is how I solved this issue for one of my projects which is also single page application in Vue and Laravel: https://github.com/lyyka/laravel-vue-blog-spa/blob/master/routes/web.php
Simply, in your routes file, you put this code:
Route::get('/{any}', function () {
return view('welcome');
})->where("any", ".*");
And in my welcome view I have:
#extends('layouts.app')
#section('content')
<div class = "container">
<router-view></router-view>
</div>
#endsection
Basically this will return your app view for any URL and so your Vue SPA should work properly. Generally, it is not a good practice to put callback functions inside your routes file, but in this case, you won't even be using your routes file as it is a SPA, so this solution can pass! :)
You should use your single html file and make a controller.
On your controller
public function index(){
return view('index');
}
on your web.php
basically, you should make the same route on your laravel and vue
Route::get('/products', [ProductsController::class,'index']);
in my vue-routes
import Products from './components/Products.vue'
{
path:'/products'
component: Products
}

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

Laravel 4: Unable to generate a URL for the named route "login" as such route does not exist

I'm creating an authorization system in my Laravel 4 project. I am trying to use the auth "before" filter.
In my routes.php file, I have:
Route::get('viewer', array('before' => 'auth', function() {
return View::make('lead_viewer');
}));
Route::get('login', 'LoginController');
The before filter calls this line in the filters.php file:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
I can manually navigate to my login route. But the auth system isn't letting this happen. I've run composer dump-autoload a couple of times, so that isn't the problem. What am I doing, since I can actually load the login page if I do it manually?
I figure it out. Laravel is looking for a named route: I had to do this:
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}));
An interesting, not very intuitive approach in Laravel. But there must be a reason Taylor did this that I'm not seeing.
To do what you were trying to do in your initial approach you could have just done:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/login');
});
and it would have worked just fine.
If you want to use named routes then you do what you posted in your answer to your own question. Essentially...more than one way to skin a cat.
Hope that helps
I know you've probably solved this by now but after stumbling across your post while trying to solve a similar problem, I wanted to share my thoughts...
Laravel is NOT looking for a named route for the guest method, it is expecting a path.
Your example works because because the named route and the path are the same i.e. "login". Try changing your URL to something other than 'login' and watch it fail.
If you want to use a named route you should use the route helper method as so...
if (Auth::guest()) return Redirect::guest( route('login') )
Hope that helps someone.

Resources