Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Middleware except with Route::group

I'm trying to create a group Route for the admin section and apply the middleware to all paths except for login and logout.

What I have so far is:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() {

    Route::resource('page', 'PageController');
    Route::resource('article', 'ArticleController');
    Route::resource('gallery', 'GalleryController');
    Route::resource('user', 'UserController');

    // ...

});

How would I declare exceptions for the middleware with the above setup?

like image 928
Sebastian Sulinski Avatar asked Feb 17 '15 16:02

Sebastian Sulinski


People also ask

How do I apply Group middleware in Laravel?

All the group routes where we apply the concept of Group middleware inside application must be needs to go with filter funnel i.e middleware and then process next. By the help of php artisan command, we create Middleware in laravel. Middlewares will be stored inside /app/Http/Middleware.

What is the difference between Route :: get and Route :: post?

Simply said, GET is usually used for presenting/viewing something, while POST is used to change something. For example, when you fetching data for some user you use GET method and it'll look something like this: Route::get('users/{id}', function($id) { $user = \App\User::find($id); echo "Name: " .

How can I call multiple middleware in route in Laravel?

You can assign multiple middlewares to Laravel route by using middleware method. Route::group(['middleware' => ['firstMiddleware','secondMiddleware']], function () { // }); This post is submitted by one of our members.


2 Answers

Simply nest groups and then you can exclude specific routes:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {

    Route::get('login', 'AuthController@login');
    Route::get('logout', 'AuthController@logout');

    Route::group(['middleware' => 'authAdmin'], function(){
        Route::resource('page', 'PageController');
        Route::resource('article', 'ArticleController');
        Route::resource('gallery', 'GalleryController');
        Route::resource('user', 'UserController');

        // ...
    });
});
like image 112
lukasgeiter Avatar answered Oct 22 '22 10:10

lukasgeiter


You can also use laravel's withoutMiddleware method as below;

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() {

  Route::resource('page', 'PageController');
  Route::resource('article', 'ArticleController');
  Route::resource('gallery', 'GalleryController');
  Route::resource('user', 'UserController');

  Route::get('login', 'AuthController@login')->withoutMiddleware([AuthAdminMiddleware::class]);
  Route::get('logout', 'AuthController@logout')->withoutMiddleware([AuthAdminMiddleware::class]);
});
like image 1
Tuncay Elvanağaç Avatar answered Oct 22 '22 09:10

Tuncay Elvanağaç