Kenny Yap
Kenny Yap

Reputation: 1337

Laravel 5 exception routes from filter in route groups

I have a quick question, suppose that I have a rout group that looks like below:

/*
|---------------------------------------------------------------------------
|   The following correspondent routes are namespaced to 'Profile'
|   and all controlles under the respective folder.
|
|   Routes are protected by milldeware 'auth'
|---------------------------------------------------------------------------
*/
Route::group(['middleware' => 'auth', 'namespace' => 'Profile'], function(){
/* Do something here */ 
});

as you can see the routes are protected by a middleware 'auth', what i wanted to ask is what if that i have few routes from that group that I do not want it to be protected by the middleware, how should i state the exception rule? can i do it like this?

/*
|---------------------------------------------------------------------------
|   The following correspondent routes are namespaced to 'Profile'
|   and all controlles under the respective folder.
|
|   Routes are protected by milldeware 'auth'
|---------------------------------------------------------------------------
*/
Route::group(['middleware' => 'auth', 'namespace' => 'Profile'], function(){
/* Do something here */ 
});


/*
|---------------------------------------------------------------------------
|   The following correspondent routes are namespaced to 'Profile'
|   and all controlles under the respective folder.
|
|   Routes are not protected by milldeware 'auth'
|---------------------------------------------------------------------------
*/
Route::group('namespace' => 'Profile', function(){
/* Do something unprotected here */ 
});

or is there an appropriate approach of doing it?

Upvotes: 1

Views: 164

Answers (1)

Sh1d0w
Sh1d0w

Reputation: 9520

The correct way is to nest the route groups like this:

Route::group(['namespace' => 'Profile'], function() {

    // Your non protected routes go here
    // ...

    Route::group(['middleware' => 'auth'], function(){
        // Your protected routes go here
        // ...
    });
});

When you nest route groups, each child group inherits all parameters from the parent one.

Upvotes: 1

Related Questions