首页 > php教程 > PHP开发 > 正文

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

高洛峰
发布: 2016-12-26 12:32:17
原创
1170 人浏览过

本文实例讲述了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(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
), array(), array(), &#39;m.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
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(&#39;project_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
), array(), array(), &#39;{project_name}.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
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(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
 &#39;subdomain&#39; => &#39;m&#39;,
), array(
 &#39;subdomain&#39; => &#39;m|mobile&#39;,
), array(), &#39;{subdomain}.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
登录后复制

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

YAML

mobile_homepage:
 path:  /
 host:  "m.{domain}"
 defaults:
  _controller: AcmeDemoBundle:Main:mobileHomepage
  domain: &#39;%domain%&#39;
 requirements:
  domain: &#39;%domain%&#39;
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(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
 &#39;domain&#39; => &#39;%domain%&#39;,
), array(
 &#39;domain&#39; => &#39;%domain%&#39;,
), array(), &#39;m.{domain}&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
登录后复制

提示

确保你总是包含了默认的选项 domain占位符,否则你需要包含 domain的值每当你使用该路由生成URL的时候。

使用包含进来的路由规则匹配

你可以设置域名选项通过导入路由配置文件,方式如下

YAML

acme_hello:
 resource: &#39;@AcmeHelloBundle/Resources/config/routing.yml&#39;
 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"), &#39;&#39;, array(), array(), array(), &#39;hello.example.com&#39;);
return $collection;
登录后复制

域名 hello.example.com 将要被设置为加载进来的新路由配置文件中的每个路由

测试你的Controllers

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

$crawler = $client->request(
 &#39;GET&#39;,
 &#39;/homepage&#39;,
 array(),
 array(),
 array(&#39;HTTP_HOST&#39; => &#39;m.&#39; . $client->getContainer()->getParameter(&#39;domain&#39;))
);
登录后复制

   

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

更多Symfony2创建基于域名的路由相关示例相关文章请关注PHP中文网!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!