在我的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
答案 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
}