symfony转发时无法检索路由丢失

时间:2014-10-29 11:41:52

标签: symfony controller routes request forward

设置三条路径

  • / first / action / {argument} #where参数可以为空
  • / second / action / {argument} #where参数是必需的
  • / third / action / {argument} #where参数也是必需的

为控制器设置三个动作

public function firstAction(Request $request, $argument=null){
    $route_params = (array) $request->attributes->get('_route_params');
    $argument=(empty($argument) ? $route_params['argument'] : $argument;
    return new Response('This was the argument'.(string)$argument);
}

public function secondAction($argument){
    return $this->forward('Bundle:Controller:first',array('argument'=>$argument));
}

public function thirdAction($argument){
     return $this->forward('Bundle:Controller:first');
}

加载以下网址时:

  • http:// localhost / first / action / passing
  • http:// localhost / second / action / forwarded
  • http:// localhost / third / action / forwarded_too

获得的结果是:

  • '这是传递的参数'
  • '这是转发的参数'
  • '这是论证'

在检查路由是否通过转发持续存在之后,我的意外是意识到转发后#"路径会从Request属性ParameterBag中丢失。

是否有其他方法可以使用参数检索请求匹配的路由?

为什么路由不会通过转发持续存在?

谢谢大家

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

forward方法取three parameters,第三个是查询参数,所以尝试修改转发,如:

public function secondAction($argument){
    return $this->forward('Bundle:Controller:first',array(),array('argument'=>$argument));
}

希望这个帮助