7

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?

Lucha Laura Hardie
  • 429
  • 1
  • 3
  • 10

5 Answers5

2

This error Route [admin] not defined is because of the route name admin is not defined.

Route name and Route path are two different things.

And you've declared the route path as admin,

Route::get('admin', 'DashboardController@index');

However,

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

means that you are redirecting the flow to the route named admin.

To sort the error,

Define a route name admin as follows in an array defined below with 'as' => 'route_name'.

Solution :

Route::get('admin', [
   'as' => 'admin',
   'uses' => 'DashboardController@index'
]);

Please refer the link : https://laravel.com/docs/master/routing#named-routes

Embedded C
  • 1,448
  • 3
  • 16
  • 29
1

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' => 'Your\Route\Authenticate',
    'auth.permissions' => 'Your\Route\AuthenticateWithPermissions'
    'permissions' => 'Your\Route\RedirectIfNoPermissions'
]
1

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...

general666
  • 1,001
  • 2
  • 16
  • 31
0

You cas use this class.

In my case, such as yours you need to set the role type permissions to your group:

Route::group(['middleware' => ['auth', 'permissions'], 'permissions' =>['Admin','Other']], function(){
   //Your Routes
}
Shapi
  • 5,493
  • 4
  • 28
  • 39
0

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.

Lucha Laura Hardie
  • 429
  • 1
  • 3
  • 10