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 "> <route path="https://www.jb51.net/" host="m.example.com"> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> </route> <route path="https://www.jb51.net/"> <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('https://www.jb51.net/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', ), array(), array(), 'm.example.com')); $collection->add('homepage', new Route('https://www.jb51.net/', 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 "> <route path="https://www.jb51.net/" host="{project_name}.example.com"> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> </route> <route path="https://www.jb51.net/"> <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('https://www.jb51.net/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', ), array(), array(), '{project_name}.example.com')); $collection->add('homepage', new Route('https://www.jb51.net/', 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 "> <route path="https://www.jb51.net/" host="{subdomain}.example.com"> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> <default key="subdomain">m</default> <requirement key="subdomain">m|mobile</requirement> </route> <route path="https://www.jb51.net/"> <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('https://www.jb51.net/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', 'subdomain' => 'm', ), array( 'subdomain' => 'm|mobile', ), array(), '{subdomain}.example.com')); $collection->add('homepage', new Route('https://www.jb51.net/', 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 "> <route path="https://www.jb51.net/" host="m.{domain}"> <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default> <default key="domain">%domain%</default> <requirement key="domain">%domain%</requirement> </route> <route path="https://www.jb51.net/"> <default key="_controller">AcmeDemoBundle:Main:homepage</default> </route> </routes>

PHP

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/734c17ee0b7070675f32653ee1a3e395.html