路由器匹配带参数的url

时间:2015-04-23 11:35:57

标签: symfony routes

在我的Symfony2项目中,我有route参数:

my_route:
    pattern:  /{param1}/{param2}
    defaults: { _controller: MyBundle:MyController:myAction }

并且在动作myAction中我得到了网址和我当我尝试通过匹配网址获取相应的路由时出现此错误:

 500 Internal Server Error - ResourceNotFoundException 

然后堆栈跟踪显示此消息:

1. in C:\Users\itaziny\git\Symfony\app\cache\dev\appDevUrlMatcher.php at line 459 

  throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();

这是我的代码:

public function myActionAction(Request $request) {
   $url = $request->headers->get('referer');
   $router = $this->get('router');
   $route = $router->match($url);
   // Some code...

   if (route == "my_route") {
       // redirect to the pag: my_route
   }
   else {
      //redirect to the page who called this action
   }
}

操作:myAction从2个不同的页面调用,我必须重定向到调用操作的页面myAction

1 个答案:

答案 0 :(得分:0)

我终于找到了答案: Symfony2: Redirecting to last route and flash a message?

所以我所做的就是这个:

$uri = $request->headers->get('referer');
$baseUrl = $request->getBaseUrl();
$lastPath = substr($uri, strpos($uri, $baseUrl) + strlen($baseUrl));

$route = $this->get('router')->match($lastPath);
if ($route['_route'] == "my_route") {
    // redirect to the pag: my_route
}
else {
  //redirect to the page who called this action

}