65

Per laravel doc, I can add the auth middleware as follows:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function ()    {
        // Uses Auth Middleware
    });

    Route::get('user/profile', function () {
        // Uses Auth Middleware
    });
});

I've also seen middleware added as follows:

Route::group(['middleware' => ['web']], function() {
  // Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
  Route::resource('blog','BlogController'); //Make a CRUD controller
});

How can I do both?

PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated

4 Answers 4

117

To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.

To use both (single middleware & middleware group) you can try this:

Route::group(['middleware' => ['auth', 'web']], function() {
  // uses 'auth' middleware plus all middleware from $middlewareGroups['web']
  Route::resource('blog','BlogController'); //Make a CRUD controller
});
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, so Route::group(['middleware' => ['web']], function() {}); and Route::group(['middleware' => 'web'], function() {}); performs the same function?
Also, so the first code snippet isn't using group middleware? The script includes Route::group(...); so I would apply to a group.
1. Yes Route::group(['middleware' => ['web']], function() {}); and Route::group(['middleware' => 'web'], function() {}); performs the same
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
You're using Route::group(...) in both cases so in both cases it will be applied to a route group, not to a single route
|
30

You may also assign multiple middleware to the route:

Route::get('/', function () {
//
})->middleware('first', 'second');

Reference

1 Comment

I had to put the multiple middlewares into an array: ...->middleware(['first', 'second']); for this to work
8

You could also do the following using the middleware static method of the Route facade:

Route::middleware(['middleware1', 'middlware2'])
    ->group(function () {
        // Your defined routes go here
    });

The middleware method accepts a single string for one middleware, or an array of strings for a group of middleware.

Comments

1
Route::middleware(['auth:api'])->middleware(['second:middleware'])
    ->prefix('yourPrefix')->group(function () {
        //Add your routes here
});

1 Comment

Please describe what you are doing and move the Laravel 7 out of the codebox ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.