你可以匹配将要来到的请求以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创建基于域名的路由(原创翻译)的更多相关文章

  1. 品尝阿里云容器服务:用nginx镜像创建容器,体验基于域名的路由机制

    在前一篇博文中我们了解了阿里云容器服务的路由机制: 请求 -> 负载均衡80端口 -> 容器主机9080端口 -> acsrouting路由容器80端口 --基于域名--> W ...

  2. Symfony2 学习笔记之系统路由

    mfony2 学习笔记之系统路由   漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...

  3. Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

    标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9 ...

  4. 8.5 Ingress实现基于域名的多虚拟主机、URL转发、及多域名https实现等案例

    1.什么是Ingress Ingress 公开了从k8s集群外部到集群内服务的 HTTP 和 HTTPS 路由. 流量路由由 Ingress 资源上定义的规则控制. 可以将 Ingress 配置为服务 ...

  5. 【原创翻译】认识MVC设计模式:web应用开发的基础(实际编码篇)

    原文地址:http://www.larryullman.com/2009/10/15/understanding-mvc-part-3/ 全系列INDEX [原创翻译]认识MVC设计模式:web应用开 ...

  6. Nginx--服务部署、基于域名的虚拟主机配置

    一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...

  7. httpd基于域名虚拟主机配置

    什么是虚拟主机 在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录. httpd支持多种方式的虚拟主机的配置,主要有以下种: 基于IP ...

  8. nginx配置基于域名、端口、IP的虚拟主机

    1.基于域名的虚拟主机: 绝大多数企业对外提供服务的网站使用的都是基于域名的主机,通过不同的域名区分不同的虚拟主机. 首先我们进入安装nginxd的目录下:/application/nginx-1.6 ...

  9. CentOS 7运维管理笔记(8)----Apache基于域名的虚拟主机配置

    使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问. (1) 在网卡 eth0的第五个接口上配置 192.168.1.215 这个地址: (2) 配置/e ...

随机推荐

  1. 前端AJAX传递数组给Springmvc接收处理

    前端传递数组后端(Spring)来接收并处理: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  2. BitMap存储jpg到手机sd卡中

    /** * 将BitMap转成Jpeg图片保存到sdcard(便于以后debug调试查看和裁切调试) */ private void saveBitmap(Bitmap bitmap, String ...

  3. python 中分支结构(switch)

    可通过字典调用:{1:case1,2:case2}.get(x,lambda *args,**key:)() # 编写一个计算器 # -*- coding=utf-8 -*- def jia(x,y) ...

  4. 【堆栈应用一】一个数divided=几个最小质因数的乘积(时间复杂度On)

    此算法由LQD提供

  5. Project2010简易操作指南[转]

    Microsoft Office Project 2010 操作手册 英文界面版 一.启动阶段 1. 前期准备 (1)新建项目文件 选择 File — NewNew 菜单, 选择项目模版 打开项目文件 ...

  6. sql server 子找父和父找子

    父找子 with RTD1 as( select Id,pid from Sys_XCode ), RTD2 as( select * from RTD1 where id=1 union all s ...

  7. Extjs改变树节点的勾选状态

    Extjs改变树节点的勾选状态 今天系统中有处地方需要一个功能点击一个按钮后将树节点前的复选框去掉,变成没有选择的状态.网上搜索了半天,然后自己查查API,终于找到解决办法了,下面把方法贴出来. 在E ...

  8. GitHub Pages 搭建流程-基于jekyll-bootstrap

    我写这篇文章的目的是记录本博客的搭建过程,自己从零开始逐步搭建起来了GitHub Pages,其中借鉴了很多的博客和模版,稍后会在后面列出,也为没有用过gihub和jekyll的童鞋提供一点帮助. 学 ...

  9. wireshark使用方法(学习笔记一)

    wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必须了解网络协议,否则就看不懂wireshark了. 为了安全考虑 ...

  10. SAP 增强-出口选找方法-全部

    ■ SAP 中如何寻找增强 方法一:利用TCODE寻找增强(第二代的增强) 执行一个程序(源代码后附),在选择屏幕处输入你所需要增强的程序TCODE,执行後,就会出现一个列表,那里就有关于如何增强这个 ...