# 路由設置
### 一、yaf路由的組成:
路由器:負責路由匹配的組件(一個應用只有一個)
路由協議:路由匹配規則(一個應用可以對應多個不同類型的規則)
### 二、路由器
1、作用:添加路由協議(路由規則)
2、主要方法:
**(1)添加路由**
添加路由一般在初始化啟動應用的時候設置(Bootstrap.php中)
添加一個新的路由:addRoute(如例1)
批量添加路由(即添加路由配置):addConfig(如例2)
**例1:給文檔分類添加分類路由**
博客分類默認的地址可能是:[http://www.hmoore.net/category?categoryName=php](http://www.hmoore.net/category?categoryName=php)
我們將它變成:[http://www.hmoore.net/category/php](http://www.hmoore.net/category/php)
~~~
$route = new \Yaf_Route_Rewrite(
"/category/:categoryName",
array(
"controller" => "product",
"action" => "category",
)
);
$router->addRoute('postCategory', $route);
~~~
**2:給文檔詳情頁添加一個路由(當然配置可以是多條的)**
文檔默認的地址可能是:[http://www.hmoore.net/posts/detail?id=122](http://www.hmoore.net/posts/detail?id=122)
注:這篇文檔的ID為122
我們為了SEO優化可能把文檔詳情頁地址換一下:[http://www.hmoore.net/posts/detail/22](http://www.hmoore.net/posts/detail/22)
首先,在項目配置文件app.ini添加相關的配置:
~~~
routes.postDetail.type="rewrite"
routes.postDetail.match="/posts/detail/:item"
;routes.postDetail.route.module=index
routes.postDetail.route.controller=posts
routes.postDetail.route.action=detail
~~~
其次,在項目啟動文件bootstrap.php中添加新的路由配置(添加\_initRoute方法):
~~~
/**
* 初始化路由
*/
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
$dispatcher->catchException(true);
$router = $dispatcher->getRouter();
$router->addConfig(\Config::get['routes']);
}
~~~
**(2)獲取路由對象**
一般在初始化啟動應用的時候獲取,一般很少使用
獲取路單個路由:getRoute:通過名稱獲取路由內容
獲取所有設置的路由:getRoutes:返回所有路由配置,默認只有Yaf\_Route\_Static
目前yaf有6種自帶的路由協議,當然,你也可以自定義協議(以下是按照使用的頻度排序的)
1、Yaf\_Route\_Static:默認路由
如:[http://www.hmoore.net/admin/posts/search?title=php](http://www.hmoore.net/admin/posts/search?title=php)
* * ?作為查詢字符串的標識與分隔點,?前端的為模塊、控制器、方法部分,?后面為查詢字符串參數部分
* * 如果有模塊,必須指明模塊(除了默認的index模塊)
2、Yaf\_Route\_Rewrite:Rewrite路由
如本文第二部分中的例子
3、Yaf\_Route\_Regex:正則路由
通過正則表達式來作為路由規則,以補充rewrite路由的不足
如:商品的詳情頁:[http://item.mall.com/item/123](http://item.mall.com/item/123)
假如它原來的地址是:[http://item.mall.com/goods/item?id=123](http://item.mall.com/goods/item?id=123)
在bootstrap.php添加配置:
~~~
/**
* 初始化路由
*/
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
$dispatcher->catchException(true);
$router = $dispatcher->getRouter();
$route = new \Yaf_Route_Regex('#^/item/([a-zA-Z-_0-9]+)#', array(
'controller'=>'goods',
'action'=>'item'
), array(1=>'id'));
$router->addRoute('goodsDetail', $route);
}
~~~
那么我們在Goods.php中的item方法中可以這樣寫:
~~~
<?php
class GoodsController extends Yaf_Controller_Abstract
{
public function itemAction()
{
var_dump($this->getRequest()->getParam('id'));
}
}
~~~
或者在配置文件app.ini添加如下的配置:
~~~
routes.itemDetail.type="regex"
routes.itemDetail.match="#^/item/([0-9]+)#"
;routes.itemDetail.route.module=index
routes.itemDetail.route.controller=goods
routes.itemDetail.route.action=item
routes.itemDetail.map.1=id
~~~
注:這里的itemDetail為規則的名稱
* type為使用的路由協議類型
* match為匹配的規則,如正則表達式
* route.module為匹配的模塊名
* route.controller為匹配的控制器名
* route.action為匹配的模塊名
* map.1表示正則或rewrite規則匹配的第一個參數的名稱
4、Yaf\\Route\\Simple:簡單路由
個人感覺這是最原始的路由,是沒有經過美化的路由,形式如:[http://www.hmoore.net/?m=admin&c=index&a=index](http://www.hmoore.net/?m=admin&c=index&a=index)
當然也可以修改參數名稱,比如module表示模塊名,controller表示控制器名,action表示方法名,即http://www.hmoore.net/?module=admin&controller=index&action=index
只需要在bootstrap.php中設置:
~~~
/**
* 初始化路由
*/
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
$dispatcher->catchException(true);
$router = $dispatcher->getRouter();
$route = new \Yaf_Route_Simple('module', 'controller', 'action');
$router->addRoute('goodsDetail', $route);
}
~~~
5、Yaf\\Route\\Supervar:static路由與simple路由的結合體
其形式為[http://www.hmoore.net/?s=/admin/index/index/name/value](http://www.hmoore.net/?s=/admin/index/index/name/value)
和Thinkphp框架的路由很類似,當然這個參數名s也是可以修改的
都需要在bootstrap.php中,添加如下配置:
~~~
/**
* 初始化路由
*/
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
$dispatcher->catchException(true);
$router = $dispatcher->getRouter();
$route = new \Yaf_Route_Supervar('s');
$router->addRoute('myRoute', $route);
}
~~~
6、Yaf\_Route\_Map:指定方法與參數分隔符的路由
Yaf\_Route\_Map議是一種簡單的路由協議, 它將REQUEST\_URI中以'/'分割的節, 組合在一起, 形成一個分層的控制器或者動作的路由結果. Yaf\_Route\_Map的構造函數接受倆個參數, 第一個參數表示路由結果是作為動作的路由結果,還是控制器的路由結果. 默認的是動作路由結果. 第二個參數是一個字符串, 表示一個分隔符, 如果設置了這個分隔符, 那么在REQUEST\_URI中, 分隔符之前的作為路由信息載體, 而之后的作為請求參數.
~~~
<?php
/**
* 對于請求request_uri為"/ap/foo/bar"
* base_uri為"/ap"
* 則最后參加路由的request_uri為"/foo/bar"
* 然后, 通過對URL分段, 得到如下分節
* foo, bar
* 組合在一起以后, 得到路由結果foo_bar
* 然后根據在構造Yaf_Route_Map的時候, 是否指明了控制器優先,
* 如果沒有, 則把結果當做是動作的路由結果
* 否則, 則認為是控制器的路由結果
* 默認的, 控制器優先為FALSE
*/
~~~
詳細路由可以參考官方文檔:[http://www.laruence.com/manual/yaf.routes.static.html](http://www.laruence.com/manual/yaf.routes.static.html)
- 序言
- 安裝 Yaf
- Yaf基礎知識
- 1.運行流程
- 2.YAF架構
- 3.目錄結構
- 4.Yaf的配置
- 5.Yaf的Bootstrap
- 6.Yaf的多模塊配置
- 7.Yaf中使用命名空間
- 本書框架配置
- 1.框架目錄結構
- 2.數據庫配置
- 3.緩存配置
- 4.全局配置
- 5.公共助手函數
- 請求與響應
- 1.請求-Request
- 2.響應-Response
- 數據庫操作
- 使用think-orm
- 接口開發
- 1.RESTful接口設計
- 2.Yar RPC接口設計
- 數據驗證
- 數據驗證 - validate
- 網頁開發
- Session
- Cookie
- 路由設置
- 工具類
- 1.Rsa加密
- 2.Random快速生成隨機數
- 3.Cache - 緩存
- 4.Weapp - 微信小程序類
- 5.Qiniu - 七牛云存儲使用
- 6.支付類(微信&支付寶)
- 7.Logs - 日志記錄