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.
-
Generate the middleware.
Terminal window php artisan module:make-middleware EnsureIsAdmin BlogThis creates
Modules/Blog/app/Http/Middleware/EnsureIsAdmin.php(Modules\Blog\Http\Middleware\EnsureIsAdmin). -
Add your authorization check in
handle().Modules/Blog/app/Http/Middleware/EnsureIsAdmin.php <?phpnamespace 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);}} -
Apply the middleware by class name in
Modules/Blog/routes/web.php.Modules/Blog/routes/web.php <?phpuse 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');}); -
Check the result. Pass
-vto show each route’s middleware.Terminal window php artisan route:list --path=posts -vGET|HEAD admin/posts ... blog.admin.posts.index › Modules\Blog\Http\Controllers\PostController@index⇂ web⇂ Modules\Blog\Http\Middleware\EnsureIsAdminGET|HEAD api/v1/posts .. api.blog.posts.index › Modules\Blog\Http\Controllers\Api\PostController@index⇂ apiGET|HEAD posts ......... blog.posts.index › Modules\Blog\Http\Controllers\PostController@index⇂ webThe
api/v1/postsroute appears if you followed the routes guide; itsapigroup does not includeEnsureIsAdmin.
Register an alias
Section titled “Register an alias”To use ->middleware('blog.admin') instead of the full class name, register an alias in 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.
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:
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.
Related
Section titled “Related”module:make-middleware: options and stub.- Module service provider: the
boot()method that the example extends. - Add routes to a module