首頁 > 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 }
登入後複製

   

兩個路由匹配相同的路徑  / ,然而第一個將只有網域名稱為m.example.com才符合

使用佔位符

這個網域選項使用佔位符的路徑比對系統。這樣就意味著你可以在你的網域中使用佔位符匹配的網域。

YAML

<?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>
登入後複製

   

XML

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 和mobile.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>
登入後複製
 

你也可以使用服務參數,如果不想將網域寫死寫法如下

YAML

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;
登入後複製

   

XML

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 }
登入後複製

  PH

提示

確保你總是包含了預設的選項domain佔位符,否則你需要包含domain的值每當你使用該路由產生URL的時候。

使用包含進來的路由規則匹配

你可以設定網域選項透過匯入路由設定檔,方式如下

YAML

 
<?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>
登入後複製

   

   

網域hello.example.com  將會被設定為載入進來的新路由設定檔中的每個路由

測試你的Controllers

你需要設定HTTP的網域頭檔在你要求的物件中,如果你想對的配對到網址在你的測試函數中

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;
登入後複製

   

希望本文所述對大家基於Symfony2框架的PHP程式設計有所幫助。

更多Symfony2創建基於域名的路由相關示例相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!