Symfony2创建基于域名的路由相关示例


Posted in PHP onNovember 14, 2016

本文实例讲述了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 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;

两个路由匹配相同的路径  / ,然而第一个将只有域名为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 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你可以按照如下方式

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

你还可以使用服务参数,如果你不想将域名写死写法如下

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 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.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 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  将要被设置为加载进来的新路由配置文件中的每个路由

测试你的Controllers

你需要设置HTTP的域名头文件在你请求的对象中,如果你想正确的匹配到网址在你的测试函数中

$crawler = $client->request(
 'GET',
 '/homepage',
 array(),
 array(),
 array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain'))
);

希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助。

PHP 相关文章推荐
phpfans留言版用到的install.php
Jan 04 PHP
php checkdate、getdate等日期时间函数操作详解
Mar 11 PHP
理解php Hash函数,增强密码安全
Feb 25 PHP
php删除与复制文件夹及其文件夹下所有文件的实现代码
Jan 23 PHP
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
Apr 01 PHP
php实现水仙花数的4个示例分享
Apr 08 PHP
Laravel框架学习笔记(二)项目实战之模型(Models)
Oct 15 PHP
php删除左端与右端空格的方法
Nov 29 PHP
彻底删除thinkphp3.1案例blog标签的方法
Dec 05 PHP
PHP的伪随机数与真随机数详解
May 27 PHP
PHP异步进程助手async-helper
Feb 05 PHP
Laravel 框架控制器 Controller原理与用法实例分析
Apr 14 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
Nov 14 #PHP
Symfony2获取web目录绝对路径、相对路径、网址的方法
Nov 14 #PHP
CodeIgniter开发实现支付宝接口调用的方法示例
Nov 14 #PHP
PHP实现无限分类的实现方法
Nov 14 #PHP
php mysql获取表字段名称和字段信息的三种方法
Nov 13 #PHP
PHP编写daemon process 实例详解
Nov 13 #PHP
php版微信小店API二次开发及使用示例
Nov 12 #PHP
You might like
7个超级实用的PHP代码片段
2011/07/11 PHP
一个PHP的QRcode类与大家分享
2011/11/13 PHP
使用淘宝IP库获取用户ip地理位置
2013/10/27 PHP
php计算到指定日期还有多少天的方法
2015/04/14 PHP
php简单统计字符串单词数量的方法
2015/06/19 PHP
Yii2 assets清除缓存的方法
2016/05/16 PHP
详解PHP版本兼容之openssl调用参数
2018/07/25 PHP
JQuery 学习笔记 element属性控制
2009/07/23 Javascript
jQuery提交多个表单的小例子
2013/06/30 Javascript
javascript实现阻止iOS APP中的链接打开Safari浏览器
2014/06/12 Javascript
JS实现一个按钮的方法
2015/02/05 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
jquery之别踩白块游戏的简单实现
2016/07/25 Javascript
Vue.js第二天学习笔记(vue-router)
2016/12/01 Javascript
JavaScript数组去重的6个方法
2017/01/21 Javascript
详解微信小程序开发之formId使用(模板消息)
2019/08/27 Javascript
vuex中遇到的坑,vuex数据改变,组件中页面不渲染操作
2020/11/16 Javascript
[04:00]DOTA2解说界神雕侠侣 CJ第四天谷子现场过生日
2013/07/30 DOTA
[55:48]VGJ.S vs TNC Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
Python中的包和模块实例
2014/11/22 Python
python中urlparse模块介绍与使用示例
2017/11/19 Python
Python制作exe文件简单流程
2019/01/24 Python
Django 多环境配置详解
2019/05/14 Python
python 模拟创建seafile 目录操作示例
2019/09/26 Python
解决python gdal投影坐标系转换的问题
2020/01/17 Python
Pandas将列表(List)转换为数据框(Dataframe)
2020/04/24 Python
Numpy中的数组搜索中np.where方法详细介绍
2021/01/08 Python
波兰在线香水店:Perfumy.pl
2019/08/12 全球购物
Kingsoft金山公司C/C++笔试题
2016/05/10 面试题
党员干部2014全国两会学习心得体会
2014/03/10 职场文书
机关中层领导干部群众路线教育实践活动个人对照检查材料
2014/09/24 职场文书
医院领导班子四风对照检查材料
2014/09/27 职场文书
2014年公路养护工作总结
2014/12/04 职场文书
技术员岗位职责
2015/02/04 职场文书
导游词之舟山普陀山
2019/11/06 职场文书
python爬不同图片分别保存在不同文件夹中的实现
2021/04/02 Python