路由器匹配带有参数的url

问题描述 投票:0回答:1

在我的

Symfony2
项目中,我有一个带有参数的
route

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

并且在操作中

myAction
我得到了url,当我尝试通过匹配url来获取相应的路线时,我得到了这个错误:

 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

php symfony routes
1个回答
0
投票

我终于在这里找到答案: Symfony2:重定向到最后一条路线并闪烁消息?

所以我所做的正是这样的:

$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

}

© www.soinside.com 2019 - 2024. All rights reserved.