The situation is as follows:

Route::group(['middleware' => 'auth'], function () { Route::resource('dashboard', 'DashboardController'); Route::group(['middleware' => 'role:moderator'], function () { Route::resources([ 'news' => 'NewsController', 'videos' => 'VideoController' ]); }); Route::group(['middleware' => 'role:admin'], function () { Route::resources([ 'users' => 'UserController', 'roles' => 'RolesController' ]); }); ... }); 

How to make some index and show methods of resource controllers, such as NewsController , VideoController , UsersController , etc., be an exception for middleware layers?

  • In the controller in the constructor, specify: $this->middleware('auth', ['except' => ['index', 'show']]); for example, then for the methods index(), show() middleware will not act .. - entithat

1 answer 1

To exclude a separate controller method from a specific middleware , in the controller's constructor you need to call

 public function __construct() { // для middleware 'guest' исключить метод 'logout' данного контроллера $this->middleware('guest')->except('logout'); }