《PHP實戰:Symfony2創建基于域名的路由相關示例》要點:
本文介紹了PHP實戰:Symfony2創建基于域名的路由相關示例,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了Symfony2創建基于域名的路由實現方法.分享給大家供大家參考,具體如下:PHP應用
你可以匹配將要來到的請求以HTTP域名的方式PHP應用
YAML方式PHP應用
mobile_homepage: path: / host: m.example.com defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
XML方式PHP應用
<?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方式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才匹配PHP應用
使用占位符PHP應用
這個域名選項使用占位符的路徑匹配系統.這樣就意味著你可以在你的域名中使用占位符匹配的域名.PHP應用
YAMLPHP應用
projects_homepage: path: / host: "{project_name}.example.com" defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
XMLPHP應用
<?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>
PHPPHP應用
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你可以按照如下方式PHP應用
YAMLPHP應用
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 }
XMLPHP應用
<?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>
PHPPHP應用
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;
你還可以使用服務參數,如果你不想將域名寫死寫法如下PHP應用
YAMLPHP應用
mobile_homepage: path: / host: "m.{domain}" defaults: _controller: AcmeDemoBundle:Main:mobileHomepage domain: '%domain%' requirements: domain: '%domain%' homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
XMLPHP應用
<?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>
PHPPHP應用
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;
提示PHP應用
確保你總是包含了默認的選項 domain占位符,否則你需要包含 domain的值每當你使用該路由生成URL的時候.PHP應用
使用包含進來的路由規則匹配PHP應用
你可以設置域名選項通過導入路由配置文件,方式如下PHP應用
YAMLPHP應用
acme_hello: resource: '@AcmeHelloBundle/Resources/config/routing.yml' host: "hello.example.com"
XMLPHP應用
<?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>
PHPPHP應用
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? 將要被設置為加載進來的新路由配置文件中的每個路由PHP應用
測試你的ControllersPHP應用
你需要設置HTTP的域名頭文件在你請求的對象中,如果你想正確的匹配到網址在你的測試函數中PHP應用
$crawler = $client->request( 'GET', '/homepage', array(), array(), array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain')) );
更多關于Symfony2相關內容感興趣的讀者可查看本站專題:《Symfony框架入門教程》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優秀開發框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP應用
希望本文所述對大家基于Symfony2框架的PHP程序設計有所幫助.PHP應用
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2795.html