Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

qwertynik's avatar

Laravel middleware is not loading

Steps to recreate the issue:

  1. Create a new Laravel 12 project. composer create-project --prefer-dist laravel/laravel test-middleware cd test-middleware
  2. Create the middleware. php artisan make:middleware TestMiddlewareLoading
  3. Create Kernel.php within app/Http folder. Paste the following contents
  1. Add this code snippet dd('middleware ran'); within the handle method of the middleware.
  2. Serve the application `php artisan serve'
  3. Visit the URL outputted.

middleware ran will NOT be printed.

Any ideas on how to get this working.

0 likes
7 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

Use bootstrap\app.php file, Here is mine for example:

Review the chapter on middle. https://laravel.com/docs/12.x/middleware#manually-managing-laravels-default-middleware-groups

Docs state:

By default, the web and api middleware groups are automatically applied to your application's corresponding routes/web.php and routes/api.php files by the bootstrap/app.php file.

2 likes
qwertynik's avatar

@jlrdw Thanks your suggestion worked. Will have to use both api and web method to register the middleware since I did not find a way to register a default middleware.

JussiMannisto's avatar

@qwertynik This is the first thing in your Kernel class:

/**
 * The application's global HTTP middleware stack.
 *
 * @var array<int, class-string|string>
 */
protected $middleware = [
	...
];

JussiMannisto's avatar

Where are you using the middleware? It's nowhere in the kernel file, and you didn't show your route files.

1 like
qwertynik's avatar

@JussiMannisto That's right. Sorry for pasting the Kernel contents wrongly. Didn't attach the routes file because there was no reason to attach it. I did not make any changes in the routes file for introducing a new middleware.

Please or to participate in this conversation.