从包含子域的 URL 中获取路由名称
Get route name from URL containing subdomain
Symfony 有方法 UrlMatcherInterface::match($pathinfo)
接受 URL 路径和 returns 相关信息(例如路由名称、路由参数、操作方法等)。因此,如果我将此方法称为 $matcher->match('/help')
,我将获得有关 http://example.com/help
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']);
Symfony 有方法 UrlMatcherInterface::match($pathinfo)
接受 URL 路径和 returns 相关信息(例如路由名称、路由参数、操作方法等)。因此,如果我将此方法称为 $matcher->match('/help')
,我将获得有关 http://example.com/help
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']);