从包含子域的URL获取路由名称
问题描述:
Symfony的方法UrlMatcherInterface::match($pathinfo)
接受URL路径并返回关于它的信息(例如路由名称,路由参数,操作方法等)。因此,如果我将此方法称为$matcher->match('/help')
,我会获得有关http://example.com/help
URL的信息。从包含子域的URL获取路由名称
问题:我如何才能获取有关包含子域的URL的信息,例如: http://subdomain.example.com/events
?此方法只接受没有主机名的路径,当我尝试将URL与主机名一起传递时,它会抛出ResourceNotFoundException
。
在此先感谢。
答
使用RequestContext & UrlMatcher找到解决方案。
$parsedUrl = parse_url($url);
$context = new RequestContext();
$context->setMethod($method); // e.g. GET
$context->setScheme($parsedUrl['scheme']);
$context->setHost($parsedUrl['host']);
$matcher = new UrlMatcher(
$this->router->getRouteCollection(),
$context
);
$routeInfo = $matcher->match($parsedUrl['path']);