-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Extending the Request class #29059
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
Comments
As you've discovered, the request is already determined at this point. I wouldnt change the request type here / ignore the current instance.. only to end up with 2 type of request instances. Shouldnt you init the custom request type at the root of bootstrapping, thus in the front controller: https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/public/index.php#L36 Then everything is OK no? |
I might have misunderstood this completely, and confused the request value resolver with the actual request on the request stack. I think I'll use the paramconverter and turn the request content into a entity instead. But just out of curiosity; If one would like to dynamically override the Request class, how would one do that? Use the event cycle to swap the request? Or should you just never override it? Editing index.php (a framework specific file) don't seem like a good idea |
Yes it is! This file is yours, it doesn't belong to the framework! |
@nicolas-grekas: Thinking of it, true. So that's the idea then. You can override the Request globally and then make use of it in index.php, but there's no way of dynamically resolving and swapping a custom request class on the stack, cause that's a bad idea (?). And to validate a request (that's what I'm trying to do), use paramconverter to convert the request content to an entity? |
Be careful that overriding |
Thanks for the clarification. But if you're not supposed to extend the Request class, can I ask why the docs are saying:
And also why the RequestValueResolver checks for subclass of the Request? |
The docs should be updated; "If you have a custom Request class, it will be injected as long as you extend the Symfony Request." should be removed. @andreaslarssen Can you submit a PR on symfony/symfony-docs?
But in any case, even if we don't recommend extending the |
Makes sense. I'll submit a PR. Thanks. |
Extending the Request class is not recommended: symfony/symfony#29059
…arssen) This PR was submitted for the 4.1 branch but it was merged into the 3.4 branch instead (closes #10628). Discussion ---------- RM: Sentence about extending the Request class Extending the Request class is not recommended: symfony/symfony#29059 Commits ------- a23feff RM: Sentence about extending the Request class
Nevertheless it was done in Symfony 5.4, where \Symfony\Component\HttpFoundation\Request is hardcoded https://github.com/symfony/runtime/blob/f7a8403ae0e6847e56881c3c106e4ea2ec4ef8c9/SymfonyRuntime.php#L123 |
Symfony runtime is designed for extensibility. It is possible to override this logic. |
Ok, I got it, I just need to create own Runtime which overrides SymfonyRuntime or GenericRuntime |
Uh oh!
There was an error while loading. Please reload this page.
I'm on symfony/framework-bundle 4.1.6 which requires symfony/http-kernel ^4.1 (and symfony/http-foundation ^4.1).
I wanted to extend the Symfony\Component\HttpFoundation\Request class, and started googling it, and found very little on the subject. I started reading the docs, and on the http-kernel page, there's a gray info box on getting the controller arguments (...in the symfony framework). The second bullet in the box reads:
So I created a custom Request class, extending the HttpFoundation Request class:
I injected the new custom Request into my controller:
Then my app crashed, saying that:
I debugged my way to the RequestValueResolver class:
...and when the request hits the supports method, the argument type is MyCustomRequest class, the $request is the HttpFoundation\Request, and the is_subclass_of test returns true.
When the request hits the resolve method, the argument type is still MyCustomRequest, the $request is still the HttpFoundation\Request, but the method returns (yields) the HttpFoundation\Request, which ends up injecting the HttpFoundation\Request class, and not MyCustomRequest, into my controller.
For now, I've added a custom RequestValueResolver, and changed it to this:
...which injects the correct Request class. Is it the right thing to do? No idea.
If I'm reading the docs or the code wrong, or if there's another way to do this, let me know. If not, this just might be a surprising bug as noone have reported it yet.
The text was updated successfully, but these errors were encountered: