php - $this->forward 丢失了用户的请求路由?

标签 php symfony routing request forward

当用户被识别但到达主页时,我想将管理员重定向到 /admin 并将成员重定向到 /member (/) .

Controller 看起来像这样:

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 开关让我得到一个很好的异常(exception):

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') .

当然,我可以通过将 return $this->forward('AppHomeBundle:Default:home'); 替换为 return $this->homeAction();< 来解决我的问题,但我不明白这一点。

Are the internal requests overwritting the user request?

注意:我使用的是 Symfony 版本 2.2.1 - app/dev/debug

编辑

查看 Symfony 的源代码,当使用 forward 时,会创建一个子请求,我们不再处于同一范围

/**
 * 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's scopes文档,他们讲述了为什么请求本身是一个范围以及如何处理它。但是他们没有说明为什么在转发时创建子请求。

更多的谷歌搜索让我找到了事件监听器,在那里我了解到可以处理子请求 ( details )。好的,对于子请求类型,但这仍然不能解释为什么用户请求只是被删除。

我的问题变成了:

Why user request is removed and not copied when forwarding?

最佳答案

因此, Controller 操作是逻辑的独立部分。这个函数彼此之间一无所知。我的答案是——单一操作处理某种特定请求(例如,使用特定的 uri prarams)。 来自 SF2 文档(http://symfony.com/doc/current/book/controller.html#requests-controller-response-lifecycle):

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 The controller from the matched route is executed and the code inside the controller creates and returns a Response object;

如果您的请求是针对路径 / 并且您想要处理此路由的内部操作(比方说 indexAction()),请执行另一个 Controller 操作(例如 fancyAction ()) 你应该为此准备fancyAction()。我的意思是使用(例如):

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 的示例:

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;
}

请注意进一步修改响应

如果你真的需要请求对象,你可以尝试:

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
}

关于php - $this->forward 丢失了用户的请求路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16382853/

相关文章:

php - MySQL 注入(inject)预防不起作用?

javascript - 动态选择表单中的值

mysql - 具有条件和限制的 Symfony/Doctrine ManyToMany

javascript - Angular 路由 : Instance Creation vs Instance Activation

php - Laravel 5.2 Post 302 重定向到 GET

java - facebook api(java或php): how to request the user to add the application when he opens it?

php - 从 SQL 表中提取照片和照片 ID,并将它们显示在 PHP 页面的列表中

验证来自表单的整数输入

javascript - Searchfuntion 在 Symfony2 中使用 ajax 调用

routing - polymer 的<app-route>元素中的query-params