本文实例讲述了symfony2创建基于域名的路由实现方法。分享给大家供大家参考,具体如下:
你可以匹配将要来到的请求以http域名的方式
yaml方式
mobile_homepage:
path: /
host: m.example.com
defaults: { _controller: acmedemobundle:main:mobilehomepage }
homepage:
path: /
defaults: { _controller: acmedemobundle:main:homepage }
xml方式
<?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>
php方式
use symfonycomponentroutingroutecollection;
use symfonycomponentroutingroute;
$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;
两个路由匹配相同的路径 / ,然而第一个将只有域名为m.example.com才匹配
使用占位符
这个域名选项使用占位符的路径匹配系统。这样就意味着你可以在你的域名中使用占位符匹配的域名。
yaml
projects_homepage:
path: /
host: "{project_name}.example.com"
defaults: { _controller: acmedemobundle:main:mobilehomepage }
homepage:
path: /
defaults: { _controller: acmedemobundle:main:homepage }
xml
<?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>
php
use symfonycomponentroutingroutecollection;
use symfonycomponentroutingroute;
$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你可以按照如下方式
yaml
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 }
xml
<?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>
php
use symfonycomponentroutingroutecollection;
use symfonycomponentroutingroute;
$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;
你还可以使用服务参数,如果你不想将域名写死写法如下
yaml
mobile_homepage:
path: /
host: "m.{domain}"
defaults:
_controller: acmedemobundle:main:mobilehomepage
domain: '%domain%'
requirements:
domain: '%domain%'
homepage:
path: /
defaults: { _controller: acmedemobundle:main:homepage }
xml
<?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>
php
use symfonycomponentroutingroutecollection;
use symfonycomponentroutingroute;
$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占位符,否则你需要包含 domain的值每当你使用该路由生成url的时候。
使用包含进来的路由规则匹配
你可以设置域名选项通过导入路由配置文件,方式如下
yaml
acme_hello:
resource: '@acmehellobundle/resources/config/routing.yml'
host: "hello.example.com"
xml
<?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>
php
use symfonycomponentroutingroutecollection;
$collection = new routecollection();
$collection->addcollection($loader->import("@acmehellobundle/resources/config/routing.php"), '', array(), array(), array(), 'hello.example.com');
return $collection;
域名 hello.example.com 将要被设置为加载进来的新路由配置文件中的每个路由
测试你的controllers
你需要设置http的域名头文件在你请求的对象中,如果你想正确的匹配到网址在你的测试函数中
$crawler = $client->request(
'get',
'/homepage',
array(),
array(),
array('http_host' => 'm.' . $client->getcontainer()->getparameter('domain'))
);
更多关于symfony2相关内容感兴趣的读者可查看本站专题:《symfony框架入门教程》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于symfony2框架的php程序设计有所帮助。
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!