Middleware is lost in FooController's Constructor when App/Http/Controller extends Illuminate/Routing/Controller #58986
Replies: 1 comment 1 reply
-
|
Hi @syahnur197, The behavior you are experiencing is due to a known distinction in Laravel regarding eager vs lazy controller instantiation. When Laravel resolves a request, it needs to gather all middlewares (from both the router and the controller itself) before it runs the middleware pipeline.
The Solution:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12.51.0
PHP Version
8.5.2
Database Driver & Version
No response
Description
Hi, as the title says. There is no middleware registered in the constructor if
App\Http\ControllerextendsIlluminate\Routing\Controller.Use case: sometimes you might need to do something in the constructor that depends on the middleware. This ability is gone if
App\Http\Controller extends Illuminate\Routing\Controller. The workaround right now is to repeat the needed procedure in all of the non constructor methods in the controller since the middlewares will be registered post-constructor.Steps To Reproduce
Scenario A
Controller.php
Web.php
FooController.php
Scenario B
Controller.php
Web.php
FooController.php
Scenario C
Controller.php
Web.php
FooController.php
looks like Illuminate Routing Controller's getMiddleware, callAction, or __call methods are causing the middlewares are not getting registered in FooController
Beta Was this translation helpful? Give feedback.
All reactions