Home » Php » php – Laravel 5 Function () not found

php – Laravel 5 Function () not found

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m putting together a site which has a protected section where users must be logged in to access. I’ve done this in Laravel 4 without too much incident. However, for the life of me I cannot figure out why I can’t get it to work in Laravel 5(L5).

In L5 middleware was/were introduced. This changes the route file to:

Route::get('foo/bar', ['middleware'=>'auth','[email protected]']);
Route::get('foo/bar/{id}', ['middleware'=>'auth','[email protected]']);

The route works fine as long as the middleware is not included.

When the route is accessed with the middleware however the result is not so much fun.

Whoops, looks like something went wrong.

ReflectionException in Route.php line 150:

Function () does not exist

Any insight, help, and/or assistance is very appreciated. I’ve done the Google circuit and couldn’t find anything relevant to my current plight. Thanks in advance.

How to&Answers:

you forgot the uses key :

Route::get('foo/bar/{id}', ['middleware'=>'auth', 'uses'=>'[email protected]']);

Answer:

If you add anything more than your controller method into your routes you need to add uses as key of the array for your controller, so for example if I don’t haw any middleware it’s enough to write:

Route::get('foo/bar', '[email protected]');
Route::get('foo/bar/{id}', '[email protected]');

However if you want to add middleware you need to write:

Route::get('foo/bar', ['middleware'=>'auth','uses' => '[email protected]']);
Route::get('foo/bar/{id}', ['middleware'=>'auth','uses' => '[email protected]']);

Answer:

In case you don’t use a controller for your view and you just want to display the view, you should do this:

Route::get('foo/bar', ['middleware' => 'auth', function () {
    return View::make('path.to.your.page');
}]);