【问题标题】:Get route name from URL containing subdomain从包含子域的 URL 获取路由名称
【发布时间】:2017-05-10 12:58:44
【问题描述】:

Symfony 有 UrlMatcherInterface::match($pathinfo) 方法,它接受 URL 路径并返回有关它的信息(例如路由名称、路由参数、操作方法等)。因此,如果我将此方法称为$matcher->match('/help'),我将获得有关http://example.com/help URL 的信息。

问题:如何获取有关包含子域的 URL 的信息,例如http://subdomain.example.com/events?此方法只接受没有主机名的路径,当我尝试使用主机名传递 URL 时,它会抛出 ResourceNotFoundException

提前致谢。

【问题讨论】:

    标签: symfony routing subdomain


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-11
      • 2016-06-15
      • 2014-08-26
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      相关资源
      最近更新 更多