PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Request attributes not injected in next middleware #42

Closed sridesmet closed 5 years ago

sridesmet commented 5 years ago

When I have an invokable class Middleware, the attributes of the previous middleware are not getting injected in the __invoke($request, $response, $next, $sampleAttribute) function. It results in:

Type: ArgumentCountError Message: Too few arguments to function ClassMiddleware::__invoke(), 3 passed and exactly 4 expected

Is this by design or not supported?

mnapoli commented 5 years ago

Both the route arguments and request attributes are supposed to be usable as controller parameters (see https://github.com/PHP-DI/Slim-Bridge/blob/master/src/ControllerInvoker.php#L44-L48), so that should work.

Could you check that that the parameters do exist in the request attirbutes?

sridesmet commented 5 years ago

This works:

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
      print($request->getAttribute('myArgument'));
}

But when passed to the__invoke function, it does not:

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next, $myArgument)
{
      print($myArgument);
}

Edit: When I edit the ControllerInvoker to print out the attribute in the $parameters array, it exists.

sridesmet commented 5 years ago

I see the group middleware is called first, then the route middleware, then the actual Handler. But the Controllerinvoker is only called on the Handler, not on the middleware.

mnapoli commented 5 years ago

Ah OK sorry, indeed that works with controllers, not middlewares right now.

sridesmet commented 5 years ago

Oh, I see. I will use $request->getAttribute() in middlewares then :)

mnapoli commented 5 years ago

👍