如何基于Host来匹配路由

3.4 版本
维护中的版本

你可以匹配传入请求(imcoming request)的HTTP host :

1
2
3
4
5
6
7
8
mobile_homepage:
    path:     /
    host:     m.example.com
    defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }

homepage:
    path:     /
    defaults: { _controller: AcmeDemoBundle:Main:homepage }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">
 
    <route id="mobile_homepage" path="/" host="m.example.com">
        <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
    </route>
 
    <route id="homepage" path="/">
        <default key="_controller">AcmeDemoBundle:Main:homepage</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
 
$collection = new RouteCollection();
$collection->add('mobile_homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
), array(), array(), 'm.example.com'));
 
$collection->add('homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:homepage',
)));
 
return $collection;

两个路由匹配相同的路径(path)/,但是第一个只在host主机是m.example.com时才算数。

使用占位符 

host选项用的是与path匹配系统相同的语法。这表明,你可以在hostname(主机名)中使用占位符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">

    <route id="projects_homepage" path="/" host="{project_name}.example.com">
        <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
    </route>

    <route id="homepage" path="/">
        <default key="_controller">AcmeDemoBundle:Main:homepage</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">
 
    <route id="projects_homepage" path="/" host="{project_name}.example.com">
        <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
    </route>
 
    <route id="homepage" path="/">
        <default key="_controller">AcmeDemoBundle:Main:homepage</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
 
$collection = new RouteCollection();
$collection->add('project_homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
), array(), array(), '{project_name}.example.com'));
 
$collection->add('homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:homepage',
)));
 
return $collection;

你还可以为这些占位符设置requirements(匹配条件)和defaults默认选项。例如,如果你要同时匹配m.example.commobile.example.com,可以使用下面代码:

1
2
3
4
5
6
7
8
9
10
11
12
mobile_homepage:
    path:     /
    host:     "{subdomain}.example.com"
    defaults:
        _controller: AcmeDemoBundle:Main:mobileHomepage
        subdomain: m
    requirements:
        subdomain: m|mobile

homepage:
    path:     /
    defaults: { _controller: AcmeDemoBundle:Main:homepage }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">
 
    <route id="mobile_homepage" path="/" host="{subdomain}.example.com">
        <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
        <default key="subdomain">m</default>
        <requirement key="subdomain">m|mobile</requirement>
    </route>
 
    <route id="homepage" path="/">
        <default key="_controller">AcmeDemoBundle:Main:homepage</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
 
$collection = new RouteCollection();
$collection->add('mobile_homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
    'subdomain'   => 'm',
), array(
    'subdomain' => 'm|mobile',
), array(), '{subdomain}.example.com'));
 
$collection->add('homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:homepage',
)));
 
return $collection;

如果你不希望写死hostname的话,可以使用服务参数(service parameters):

1
2
3
4
5
6
7
8
9
10
11
12
mobile_homepage:
    path:     /
    host:     "m.{domain}"
    defaults:
        _controller: AcmeDemoBundle:Main:mobileHomepage
        domain: '%domain%'
    requirements:
        domain: '%domain%'

homepage:
    path:  /
    defaults: { _controller: AcmeDemoBundle:Main:homepage }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
 
    <route id="mobile_homepage" path="/" host="m.{domain}">
        <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
        <default key="domain">%domain%</default>
        <requirement key="domain">%domain%</requirement>
    </route>
 
    <route id="homepage" path="/">
        <default key="_controller">AcmeDemoBundle:Main:homepage</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
 
$collection = new RouteCollection();
$collection->add('mobile_homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
    'domain' => '%domain%',
), array(
    'domain' => '%domain%',
), array(), 'm.{domain}'));
 
$collection->add('homepage', new Route('/', array(
    '_controller' => 'AcmeDemoBundle:Main:homepage',
)));
 
return $collection;

你应确保对domain占位符设置一个默认选项,否则,每当你使用路由(名称)来生成一个URL时都需要附带这个domain的取值。

被导入路由的Host匹配 

你同样可以对导入的路由设置host选项:

1
2
3
acme_hello:
    resource: '@AcmeHelloBundle/Resources/config/routing.yml'
    host:     "hello.example.com"
1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
 
    <import resource="@AcmeHelloBundle/Resources/config/routing.xml" host="hello.example.com" />
</routes>
1
2
3
4
5
6
use Symfony\Component\Routing\RouteCollection;
 
$collection = new RouteCollection();
$collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), '', array(), array(), array(), 'hello.example.com');
 
return $collection;

hello.example.com主机名将在“每个路由从全新routing资源中加载进来”时被设置(到路由中)。

测试你的控制器 

如果你要在功能测试(functional tests)中取得(与路由)相匹配的past url(译注:past可能是指“刚才”被用来测试的url),你需要为request对象设置Host HTTP头。

1
2
3
4
5
6
7
$crawler = $client->request(
    'GET',
    '/homepage',
    array(),
    array(),
    array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain'))
);

本文,包括例程代码在内,采用的是 Creative Commons BY-SA 3.0 创作共用授权。

登录symfonychina 发表评论或留下问题(我们会尽量回复)