$ this-> forward会释放用户请求的路线? [英] $this->forward looses the user's requested route?
问题描述
我想在确定用户身份但将用户转到后将管理员重定向到 / admin
,将成员重定向到 / member
。主页( /
)。
I want to redirect admins to /admin
and members to /member
when users are identified but get to the home page (/
).
控制器看起来像这样:
public function indexAction()
{
if ($this->get('security.context')->isGranted('ROLE_ADMIN'))
{
return new RedirectResponse($this->generateUrl('app_admin_homepage'));
}
else if ($this->get('security.context')->isGranted('ROLE_USER'))
{
return new RedirectResponse($this->generateUrl('app_member_homepage'));
}
return $this->forward('AppHomeBundle:Default:home');
}
如果我的用户已登录,则效果很好,没问题。但是,如果不是这样,我的i18n开关将使我得到一个很好的异常:
If my users are logged in, it works well, no problem. But if they are not, my i18n switch makes me get a nice exception :
合并过滤器仅适用于$ b中的数组或哈希$ b AppHomeBundle:Default:home.html.twig。
The merge filter only works with arrays or hashes in "AppHomeBundle:Default:home.html.twig".
崩溃的行:
{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'fr'})) }}
如果我查看 app.request。 get('_ route_params')
,它为空,还有 app.request.get('_ route')
。
If I look at the app.request.get('_route_params')
, it is empty, as well as app.request.get('_route')
.
当然,我可以通过替换 return $ this-&forward('AppHomeBundle:Default:home');
来解决我的问题通过 return $ this-> homeAction();
,但我不明白这一点。
Of course, I can solve my problem by replacing return $this->forward('AppHomeBundle:Default:home');
by return $this->homeAction();
, but I don't get the point.
内部请求是否覆盖用户请求?
Are the internal requests overwritting the user request?
注意:我正在使用 Symfony版本2.2.1-app / dev / debug
查看Symfony的源代码,当使用 forward
时,将创建一个子请求,并且我们不再位于同一 scope 中。
Looking at the Symfony's source code, when using forward
, a subrequest is created and we are not in the same scope anymore.
/**
* Forwards the request to another controller.
*
* @param string $controller The controller name (a string like BlogBundle:Post:index)
* @param array $path An array of path parameters
* @param array $query An array of query parameters
*
* @return Response A Response instance
*/
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);
}
通过查看 Symfony2的作用域文档,它们说明了为什么请求本身就是作用域以及如何处理它。但是他们并没有说明转发时为什么会创建子请求。
By looking at the Symfony2's scopes documentation, they tell about why request is a scope itself and how to deal with it. But they don't tell about why sub-requests are created when forwarding.
更多的谷歌搜索使我进入了事件侦听器,在那我了解到可以处理子请求(详细信息)。好的,对于子请求类型,但这仍然无法解释为什么只是删除了用户请求。
Some more googling put me on the event listeners, where I learnt that the subrequests can be handled (details). Ok, for the sub-request type, but this still does not explain why user request is just removed.
我的问题变成了:
为什么转发时用户请求被删除而不被复制?
Why user request is removed and not copied when forwarding?
推荐答案
因此,控制器动作是逻辑的独立部分。此功能彼此之间一无所知。我的答案是-单个动作句柄的特定请求(例如特定的uri prarams)。来自SF2文档的
( http:// symfony .com / doc / current / book / controller.html#requests-controller-response-lifecycle ):
So, controller actions are separated part of logic. This functions doesn't know anything about each other. My answer is - single action handle kind of specific request (e.g. with specific uri prarams). From SF2 docs (http://symfony.com/doc/current/book/controller.html#requests-controller-response-lifecycle):
2路由器从请求中读取信息(例如URI),找到与该信息匹配的
路由,并从该路由中读取_controller
参数;
2 The Router reads information from the request (e.g. the URI), finds a route that matches that information, and reads the _controller parameter from the route;
3匹配路径中的控制器已执行
,控制器内部的代码创建并返回了
响应对象;
3 The controller from the matched route is executed and the code inside the controller creates and returns a Response object;
如果您的请求是路径 /
,而您想进行内部操作(比如说 indexAction()
)处理此路由,执行另一个控制器操作(例如 fancyAction()
),则应准备 fancyAction()
。我的意思是关于使用(例如):
If your request is for path /
and you wanna inside action (lets say indexAction()
) handling this route, execute another controller action (e.g. fancyAction()
) you should prepare fancyAction()
for that. I mean about using (e.g.):
public function fancyAction($name, $color)
{
// ... create and return a Response object
}
相反:
public function fancyAction()
{
$name = $this->getRequest()->get('name');
$color = $this->getRequest()->get('color');
// ... create and return a Response object
}
示例来自sf2 dosc:
Example from sf2 dosc:
public function indexAction($name)
{
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green',
));
// ... further modify the response or return it directly
return $response;
}
请注意进一步修改响应
。
如果您确实需要请求对象,则可以尝试:
If you really need request object, you can try:
public function indexAction()
{
// prepare $request for fancyAction
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array('request' => $request));
// ... further modify the response or return it directly
return $response;
}
public function fancyAction(Request $request)
{
// use $request
}
这篇关于$ this-> forward会释放用户请求的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!