2017-05-10 79 views
0

Symfony的方法UrlMatcherInterface::match($pathinfo)接受URL路徑並返回關於它的信息(例如路由名稱,路由參數,操作方法等)。因此,如果我將此方法稱爲$matcher->match('/help'),我會獲得有關http://example.com/help URL的信息。從包含子域的URL獲取路由名稱

問題:我如何才能獲取有關包含子域的URL的信息,例如: http://subdomain.example.com/events?此方法只接受沒有主機名的路徑,當我嘗試將URL與主機名一起傳遞時,它會拋出ResourceNotFoundException

在此先感謝。

回答

0

使用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']);