mobile_homepage:
path: /
host: m.example.com
defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
homepage:
path: /
defaults: { _controller: AcmeDemoBundle:Main:homepage }

<?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>

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;

使用占位符
这个域名选项使用占位符的路径匹配系统。这样就意味着你可以在你的域名中使用占位符匹配的域名。YAML
projects_homepage:
path: /
host: "{project_name}.example.com"
defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
homepage:
path: /
defaults: { _controller: AcmeDemoBundle:Main:homepage }

<?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>

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;

m.example.com
和mobile.example.com
你可以按照如下方式 YAMLmobile_homepage:
path: /
host: "{subdomain}.example.com"
defaults:
_controller: AcmeDemoBundle:Main:mobileHomepage
subdomain: m
requirements:
subdomain: m|mobile
homepage:
path: /
defaults: { _controller: AcmeDemoBundle:Main:homepage }

<?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>

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;

mobile_homepage:
path: /
host: "m.{domain}"
defaults:
_controller: AcmeDemoBundle:Main:mobileHomepage
domain: '%domain%'
requirements:
domain: '%domain%'
homepage:
path: /
defaults: { _controller: AcmeDemoBundle:Main:homepage }

<?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>

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;

使用包含进来的路由规则匹配 你可以设置域名选项通过导入路由配置文件,方式如下YAML
acme_hello:
resource: '@AcmeHelloBundle/Resources/config/routing.yml'
host: "hello.example.com"

<?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>

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;

测试你的Controllers
你需要设置HTTP的域名头文件在你请求的对象中,如果你想正确的匹配到网址在你的测试函数中
$crawler = $client->request(
'GET',
'/homepage',
array(),
array(),
array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain'))
);