好的,你可以获得当前的路线名称,app.request.attributes.get('_route')但是不可能从网址获取?
有点像app.request.attributes.get('/about')?
Wou*_*r J 15
您可以使用Router类/服务:
public function indexAction()
{
$router = $this->get('router');
$route = $router->match('/foo')['_route'];
}
Run Code Online (Sandbox Code Playgroud)
文档中的更多信息
我最近发现match()方法使用当前请求的HTTP METHOD来匹配请求.因此,如果您正在执行PUT请求,它将尝试将您给出的URL与PUT方法匹配,从而导致MethodNotAllowedException异常(例如,获取引用).
为了避免这种情况,我正在使用此解决方法:
// set context with GET method of the previous ajax call
$context = $this->get('router')->getContext();
$currentMethod = $context->getMethod();
$context->setMethod('GET');
// match route
$routeParams = $this->get('router')->match($routePath);
// set back original http method
$context->setMethod($currentMethod);
Run Code Online (Sandbox Code Playgroud)
然而,它总是一个GET请求可能不是真的.在您的情况下,它可能是一个POST请求.
我已将此问题发送给Symfony社区.让我们看看他们的建议.
小智 5
使用绝对路径时,即使使用匹配方法,我也得到MethodNotAllowed,我像这样解决它
$ref = str_replace("app_dev.php/", "", parse_url($request->headers->get('referer'),PHP_URL_PATH ));
$route = $this->container->get('router')->match($ref)['_route'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15765 次 |
| 最近记录: |