-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add-route-arguments-as-attributes-to-the-request-handler-strategy #2773
add-route-arguments-as-attributes-to-the-request-handler-strategy #2773
Conversation
I’m not sure I’m in favour of doing this. Why can’t we add this to |
@l0gicgate please don't, cause it make the code of the request handler depending on the framework. Expressive and my own work with attribute for the arguments, it seems to be common practice. |
How does that make it dependant though? You can already retrieve the route arguments via the To me filling up the request’s attributes with a for loop seems like object pollution. I don’t like the “shove it all in there” approach without structure. With the default strategy it’s contained in |
RouteContext is Slim only, using PSR15 and still depend on the framework is useless to me. Strategy is not changeable, see Line 366 in 2b0ed80
|
I think by default we should leave the route arguments in the But feel free to create your own route strategy (https://www.slimframework.com/docs/v4/objects/routing.html#route-strategies) or build a middleware that does exactly what you need. // Middleware (needs to be called after the routing middleware and therefore has to be added before)
$app->add(function (Request $request, \Psr\Http\Server\RequestHandlerInterface $requestHandler) {
/** @var Slim\Routing\Route\Route $route */
$route = $request->getAttribute('route');
foreach ($route->getArguments() as $key => $val) {
$request = $request->withAttribute($key, $val);
}
return $requestHandler->handle($request);
}); Just make sure, that you would not overwrite existing attributes! |
@adriansuter as mentioned in my previous post gets the strategy overridden if the controller implements the RequestHandlerInterface. Middleware would work. But I don't see the point why you're not interested in being compatible with other microframeworks work with PSR15. |
I couldn't find any information in the specs of PSR-15 about storing the route placeholders (arguments) in the server request. Could you give me a link? Or is this some kind of common silent agreement between many microframeworks? If yes, do you have a list of microframeworks that support this kind of behaviour? |
@adriansuter it’s not common practice. And as I said, this can be achieved with creating a different invocation strategy which we could include for users to use if they wanted too. Then you can set the default invocation strategy with Every route you map thereafter will have that strategy and do what you want. |
Please read the mentioned if statement to see, that changing the strategy is not possible without a code change. About common: path values as attribute was default in Slim 3 and is in Expressive. https://github.com/slimphp/Slim/blob/3.x/Slim/Handlers/Strategies/RequestResponse.php#L36 About middleware: Adding a middleware would mean a general behavior change cause I would need to route first. About Route Context: this context exists only in slim, so I loose the benefits of PSR15. |
@dominikzogg we need to change the logic in the |
@l0gicgate thank you |
Follow discussion in #2774 |
No description provided.