【问题标题】:symfony Can't retrieve route gets lost when forwardingsymfony 无法检索路由在转发时丢失
【发布时间】:2014-10-29 11:41:52
【问题描述】:

假设三个路径

  • /first/action/{argument} #where 参数可以为空
  • /second/action/{argument} #需要参数的地方
  • /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/passed
  • http://localhost/second/action/forwarded
  • http://localhost/third/action/forwarded_too

获得的结果是:

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

在检查路由是否通过转发保留后,我惊讶地意识到“路由在转发后从请求属性ParameterBag中丢失”。

有没有其他方法可以通过参数检索请求匹配的路由?

为什么路由不通过转发保留?

谢谢大家

【问题讨论】:

    标签: symfony controller routes request forward


    【解决方案1】:

    【讨论】:

      【解决方案2】:

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

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

      希望有帮助

      【讨论】:

      • 感谢您的回复,但这不是我想要的。我想知道的是保留路由,让它通过转发持续存在,因为没有其他请求发生,这没有任何意义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 2022-08-17
      • 1970-01-01
      • 2012-07-12
      • 2022-08-22
      • 1970-01-01
      • 2019-02-17
      相关资源
      最近更新 更多