Skip to content

Add middleware to a module

To protect routes in the Blog module with custom middleware, generate the class and apply it in the module’s route file. You need an enabled Blog module with a route (see Add routes to a module).

Laravel 13 has no app/Http/Kernel.php. Apply middleware by class name or register an alias in bootstrap/app.php.

  1. Generate the middleware.

    Terminal window
    php artisan module:make-middleware EnsureIsAdmin Blog

    This creates Modules/Blog/app/Http/Middleware/EnsureIsAdmin.php (Modules\Blog\Http\Middleware\EnsureIsAdmin).

  2. Add your authorization check in handle().

    Modules/Blog/app/Http/Middleware/EnsureIsAdmin.php
    <?php
    namespace Modules\Blog\Http\Middleware;
    use Closure;
    use Illuminate\Http\Request;
    class EnsureIsAdmin
    {
    public function handle(Request $request, Closure $next)
    {
    abort_unless($request->user()?->is_admin, 403);
    return $next($request);
    }
    }
  3. Apply the middleware by class name in Modules/Blog/routes/web.php.

    Modules/Blog/routes/web.php
    <?php
    use Illuminate\Support\Facades\Route;
    use Modules\Blog\Http\Controllers\PostController;
    use Modules\Blog\Http\Middleware\EnsureIsAdmin;
    Route::get('posts', [PostController::class, 'index'])->name('blog.posts.index');
    Route::middleware(EnsureIsAdmin::class)->group(function () {
    Route::get('admin/posts', [PostController::class, 'index'])->name('blog.admin.posts.index');
    });
  4. Check the result. Pass -v to show each route’s middleware.

    Terminal window
    php artisan route:list --path=posts -v
    GET|HEAD admin/posts ... blog.admin.posts.index › Modules\Blog\Http\Controllers\PostController@index
    ⇂ web
    ⇂ Modules\Blog\Http\Middleware\EnsureIsAdmin
    GET|HEAD api/v1/posts .. api.blog.posts.index › Modules\Blog\Http\Controllers\Api\PostController@index
    ⇂ api
    GET|HEAD posts ......... blog.posts.index › Modules\Blog\Http\Controllers\PostController@index
    ⇂ web

    The api/v1/posts route appears if you followed the routes guide; its api group does not include EnsureIsAdmin.

To use ->middleware('blog.admin') instead of the full class name, register an alias in bootstrap/app.php:

bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
$middleware->alias([
'blog.admin' => \Modules\Blog\Http\Middleware\EnsureIsAdmin::class,
]);
})

To keep the alias inside the module, add a boot() method to the module service provider. Always call parent::boot(): without it, the module does not register its commands, scheduled commands, translations, views, health indicators, or migrations.

Modules/Blog/app/Providers/BlogServiceProvider.php
use Illuminate\Support\Facades\Route;
use Modules\Blog\Http\Middleware\EnsureIsAdmin;
public function boot(): void
{
parent::boot();
Route::aliasMiddleware('blog.admin', EnsureIsAdmin::class);
}

Apply the alias in your routes:

Modules/Blog/routes/web.php
Route::get('admin/posts', [PostController::class, 'index'])->middleware('blog.admin');

The module’s RouteServiceProvider already wraps routes/web.php in the web middleware group and routes/api.php in the api group.