欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Symfony2創(chuàng)建基于域名的路由相關(guān)示例

 更新時(shí)間:2016年11月14日 10:39:31   作者:陳達(dá)輝  
這篇文章主要介紹了Symfony2創(chuàng)建基于域名的路由,結(jié)合實(shí)例形式分析了Symfony2路由的創(chuàng)建技巧,需要的朋友可以參考下

本文實(shí)例講述了Symfony2創(chuàng)建基于域名的路由實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

你可以匹配將要來(lái)到的請(qǐng)求以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;

兩個(gè)路由匹配相同的路徑  / ,然而第一個(gè)將只有域名為m.example.com才匹配

使用占位符

這個(gè)域名選項(xiàng)使用占位符的路徑匹配系統(tǒng)。這樣就意味著你可以在你的域名中使用占位符匹配的域名。

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;

你還可以為這些占位符設(shè)置條件和默認(rèn)的選項(xiàng)。列如,如果你想匹配  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;

你還可以使用服務(wù)參數(shù),如果你不想將域名寫(xiě)死寫(xiě)法如下

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;

提示

確保你總是包含了默認(rèn)的選項(xiàng) domain占位符,否則你需要包含 domain的值每當(dāng)你使用該路由生成URL的時(shí)候。

使用包含進(jìn)來(lái)的路由規(guī)則匹配

你可以設(shè)置域名選項(xiàng)通過(guò)導(dǎo)入路由配置文件,方式如下

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  將要被設(shè)置為加載進(jìn)來(lái)的新路由配置文件中的每個(gè)路由

測(cè)試你的Controllers

你需要設(shè)置HTTP的域名頭文件在你請(qǐng)求的對(duì)象中,如果你想正確的匹配到網(wǎng)址在你的測(cè)試函數(shù)中

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

更多關(guān)于Symfony2相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Symfony框架入門(mén)教程》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《ThinkPHP入門(mén)教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Symfony2框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論