Search code examples
laravelmiddleware

$request-merge() disappears in parent controller


I'm stuck on a weird issue...

I have a simple middleware.

$request->merge([
   'user' => $jwt->toArray(),
   'app_version' => $request->header('app-version')
]);

When I do a dump right before the $next($request); the it has the user object and the app_version. All good! See image below:

enter image description here

Moving to the controller. When in the function of defined in the route, and doing a dump($request); it has the user Object

enter image description here

The InstallController extends the controller.php which extends the use Illuminate\Routing\Controller as BaseController;

In the controller.php we have a construct function.

enter image description here

For some reason here the $request is empty and lost the object.

enter image description here

Why is this happening? And why is this available in the InstallController but not in the parent?


Solution

  • What turned out is the following. In Laravel lumen, controller constructors are called after the middleware has completed the request. In Laravel the constructors are called before the middleware.

    Meaning that adding params to the request in the middleware in Laravel doesn't work.

    Thanks for pointing me in the right direction.