How to group middleware in Laravel

In Laravel, middleware is used to filter HTTP requests entering our application. It allows us to perform actions before or after the request is handled by our application's routes. Grouping middleware allows us to apply multiple middleware to a group of routes, making our code more organized and easier to manage. Here's how we can group middleware in Laravel:

Defining our middleware

First, we need to create the middleware that we want to group. We can generate a new middleware using the following Artisan command:

php artisan make:middleware MyMiddleware

This will create a new middleware file under the app/Http/Middleware directory. In this example, the file will be named the MyMiddleware.php. We can implement the necessary logic in the default handle() method. This logic will be executed before or after the request is handled by the application's routes.

Register our middleware

Next, we should register our middleware in the $routeMiddleware array in the app/Http/Kernel.php file. This file contains all the middleware that can be applied to our application's routes. Add the following code to the $routeMiddleware array:

protected $routeMiddleware = [
// other middleware...
'my-middleware' => \App\Http\Middleware\MyMiddleware::class,
];

Grouping middleware in routes

Our middleware is registered so that we can group them in our routes. The most common place to do this is in the routes/web.php or routes/api.php file.

We can group middleware using the middleware() method, passing the middleware key defined in the Kernel and separated by an array:

Route::group(['middleware' => ['my-middleware', 'another-middleware']], function () {
// Your routes here
});

Alternatively, we can apply middleware to individual routes within the group:

Route::group(['middleware' => ['my-middleware']], function () {
Route::get('/example', 'ExampleController@index');
Route::post('/example', 'ExampleController@store');
});

In the first example, both the my-middleware and another-middleware will be applied to all the routes within the group.

Remember, middleware groups can be applied to both web and API routes. If you have a specific set of middleware that needs to be applied to all your API routes, you can group them in the routes/api.php file.

That's it! Our middleware will be applied to all routes defined within the respective group, providing better organization and reusability of your middleware logic.

Note: The most recent version of Laravel, version 9.26.1, was used when writing the following example. 

Try it out

The output of the coding example can be observed after clicking the "Run" button.

php:
  preset: laravel
  version: 8
  disabled:
    - no_unused_imports
  finder:
    not-name:
      - index.php
      - server.php
js:
  finder:
    not-name:
      - webpack.mix.js
css: true
Playground to group middleware in Laravel

Code explanation

In the above playground, there is a Laravel project to group middleware. ExampleController is the main controller file.

  • Line 3: The path is defined.

  • Lines 57: Include the required libraries to run the project.

  • Lines 816: The ExampleController class is defined to display the output. On line 14, the view is returned to example.blade.php file located at the resources/views/ path. This file sets the view to be displayed on the output.

In the coding example above, we define our middleware, creating a new middleware file under the app/Http/Middleware directory. We register the middleware in the $routeMiddleware array in the app/Http/Kernel.php file and group middleware in routes in the routes/web.php file.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved