How to Add More than One Item to Middleware on Route in Laravel 5

I recently started using Laravel 5 and I'm having a lot of trouble implementing a system that not only authorizes users, but also checks permissions.

In all of the examples I've dug up online, I see two items being applied as middleware. For example:

Route::group(['middleware' => ['auth', 'permissions']], function() {
  // protected routes here
  Route::get('admin', 'DashboardController@index');
});

However, I cannot get this to work no matter what I do. I can only apply one item as middleware, such as:

Route::group(['middleware' => 'auth'], function() {
  // protected routes here
  Route::get('admin', 'DashboardController@index');
});

If I apply two, I get the error "Route [admin] not defined."

I have tried everything I can think of, and I am banging my head against a brick wall. How on earth can I apply two or more items of middleware to one route?


You might juste try to create one middleware doing more than on verification ?

In your Kernel.php you might have something like :

protected $routeMiddleware = [
    'auth' => 'YourRouteAuthenticate',
    'auth.permissions' => 'YourRouteAuthenticateWithPermissions'
    'permissions' => 'YourRouteRedirectIfNoPermissions'
]

I think you have error in brackets. Your code should looks like:

Route::group(['middleware' => ['auth', 'permissions'], function() {
     // protected routes here
     Route::get('admin', 'DashboardController@index');
}]);

Check the closing bracket...


I am answering my own question as many people miss the comment where the solution is mentioned.

The issue was in the Permissions middleware, as mentioned in the comment made by lukasgeiter.

As seen in my reply, the answer was found in the permissions middleware, where was using:

return redirect()->route('admin'); 

instead of:

redirect('admin'); 

There was actually nothing wrong with the code in my routes.php file.

链接地址: http://www.djcxy.com/p/83996.html

上一篇: transclude不起作用

下一篇: 如何在Laravel 5的路由中添加多个项目到中间件