Symfony2创建基于域名的路由相关示例
网络编程 2021-07-05 08:23www.168986.cn编程入门
这篇文章主要介绍了Symfony2创建基于域名的路由,结合实例形式分析了Symfony2路由的创建技巧,需要的朋友可以参考下
本文实例讲述了Symfony2创建基于域名的路由实现方法。分享给大家供大家参考,具体如下:
你可以匹配将要来到的请求以HTTP域名的方式
YAML方式
mobile_homepage: path: / host: m.example. defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
XML方式
<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony./schema/routing" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://symfony./schema/routing http://symfony./schema/routing/routing-1.0.xsd"> <route id="mobile_homepage" path="/" host="m.example."> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> </route> <route id="homepage" path="/"> <default key="_controller">AcmeDemoBundle:Main:homepage</default> </route> </routes>
PHP方式
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.')); $collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage', ))); return $collection;
两个路由匹配相同的路径 / ,然而第一个将只有域名为m.example.才匹配
使用占位符
这个域名选项使用占位符的路径匹配系统。这样就意味着你可以在你的域名中使用占位符匹配的域名。
YAML
projects_homepage: path: / host: "{project_name}.example." defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
XML
<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony./schema/routing" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://symfony./schema/routing http://symfony./schema/routing/routing-1.0.xsd"> <route id="projects_homepage" path="/" host="{project_name}.example."> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> </route> <route id="homepage" path="/"> <default key="_controller">AcmeDemoBundle:Main:homepage</default> </route> </routes>
PHP
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.')); $collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage', ))); return $collection;
你还可以为这些占位符设置条件和默认的选项。列如,如果你想匹配 m.example. 和mobile.example.你可以按照如下方式
YAML
mobile_homepage: path: / host: "{subdomain}.example." 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./schema/routing" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://symfony./schema/routing http://symfony./schema/routing/routing-1.0.xsd"> <route id="mobile_homepage" path="/" host="{subdomain}.example."> <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 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.')); $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./schema/routing" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://symfony./schema/routing http://symfony./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 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占位符,否则你需要包含 domain的值每当你使用该路由生成URL的时候。
使用包含进来的路由规则匹配
你可以设置域名选项通过导入路由配置文件,方式如下
YAML
acme_hello: resource: '@AcmeHelloBundle/Resources/config/routing.yml' host: "hello.example."
XML
<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony./schema/routing" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://symfony./schema/routing http://symfony./schema/routing/routing-1.0.xsd"> <import resource="@AcmeHelloBundle/Resources/config/routing.xml" host="hello.example." /> </routes>
PHP
use Symfony\Component\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), '', array(), array(), array(), 'hello.example.'); return $collection;
域名 hello.example. 将要被设置为加载进来的新路由配置文件中的每个路由
测试你的Controllers
你需要设置HTTP的域名头文件在你请求的对象中,如果你想正确的匹配到网址在你的测试函数中
$crawler = $client->request( 'GET', '/homepage', array(), array(), array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain')) );
更多关于Symfony2相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程