2013-02-28 31 views
1

当我在控制器中执行forward()时,我失去了我的路由和route_parameters。Symfony2为什么前向嵌套请求对象?

当我有ParentAction时,它会转发到ChildAction。在Childaction中,我做return $this->render('myTemplate.html.twig', array());然后请求属性被嵌套!

所以当模板得到渲染,而不是$request['attributes']['_route_parameters']我得到$request['attributes']['request']['attributes']['_route_parameters']

尽管在ChildAction中,当我做$this->getRequest();时,hierarchie是正常的。

这是一个错误,还是我做错了什么?

回答

0

可能的解决方案是在转发时传递请求作为第二个参数。

$response = $this->forward('MyBundle:MyController:myAction', array('request' => $request)); 

此外,作为前锋仅仅是核心Symfony2的功能的快捷方式,可以this可能帮助。

+0

你可以检查我的编辑?我可以阅读请求等,但有一些奇怪的事情发生。 – Nealv 2013-02-28 10:20:50

0

在我的情况下面的代码帮助:

$subRequest = $this->container->get('request')->duplicate(
    array(), 
    null, 
    array('topicId' => $topicId,'_controller' => 'SomeBundle:Topic:close')); 

return $this->container->get('http_kernel') 
    ->handle($subRequest, HttpKernelInterface::SUB_REQUEST); 

“主题”是TopicController和“亲密”是closeAction

1

的原因是,symfony的不假设你有相同的路由参数。 因此,当您前进时,即使它们相同,您仍需要重新提供新路线所需的路线参数。

(顺便说一句,你还必须为新路线的查询参数。)

public function indexAction($param) 
{ 

    return $this->forward(
     'AppBundle\\Controller\\DefaultController::otherAction', 
     array("someOtherParam" => $param), 
     $request->query->all() // Causes query string params to be copied 
    ); 
} 

// This route has a different parameter. 
public function otherAction($someOtherParam) ...