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
the route name "app.request.attributes.get('_route')" #5804
Comments
it is, as you are not relying on the routing in your forwarded request but setting the controller directly. |
exactly, so what is the method to retrieve this information in the template without changing my action |
Well, if your subrequest needs to know some informations about the main request, you need to pass them explicitly. they are separate requests |
oki thx; |
Very interesting. User enters "http://www.mysite.com/path1", controller makes a forward, and route is lost. {{ app.request.get('_route') }} shows nothing, though in browser address we see "http://www.mysite.com/path1" Language switchers stop working In my opinion - bug. Forward returns a Response, therefore preserving _route and get parameters is necessary. |
Well, a forward is a subrequest. And your subrequest receive the Request object of the subrequest, not of the master request. There is no reason to have the route of the master request in the subrequest object. |
deleted - see comments below |
@rivaros did you read the last sentence of my message ? |
Christophe, definitely. I was a little misleading in my comments, but what I really mean is:
public function forward($controller, array $path = array(), array $query = array())
{
$path['_controller'] = $controller;
$subRequest = $this->container->get('request')->duplicate($query, null, $path);
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
} It duplicates the POST parameters, and also GET parameters, if they are not specified in forward call. Now point 3: public function forward($controller, array $path = array(), array $query = array())
{
$path['_controller'] = $controller;
+ $path['_route'] = $this->container->get('request')->attributes->get('_route');
+ $path['_route_params'] = $this->container->get('request')->attributes->get('_route_params');
$subRequest = $this->container->get('request')->duplicate($query, null, $path);
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
} By adding 2 lines of code you can solve These are attributes, no routing really involved. |
When using forwarding end-user still sees the URL of parent request in his browser. |
@rivaros subrequest are not done by the browser, be it when using forwarding or when using the FragmentHandler. The browser does not even know about subrequests. |
Christophe, where did i say they're done by browser? For those who need to solve issue with forward, a workaround return $this->forward('Yourbundle:Controller:action',
array(
... your parameters...,
'_route' => $this->getRequest()->attributes->get('_route'),
'_route_params' => $this->getRequest()->attributes->get('_route_params');
)); ) |
What the user sees is not relevant for the sub-request. the client of the sub-request is not the browser. It is your PHP code handling the master request. |
@rivaros, thanks for workaround! |
@rivaros, thanks for the solution. |
Another workaround is to class FirstController extends SecondController {
function indexAction (Request $request) {
parent::indexAction($request);
}
} |
When i use the forward request in my "action" the route name "app.request.attributes.get('_route')" is empty is that normal?
The text was updated successfully, but these errors were encountered: