詳解php框架Yaf路由重寫
通常為了友好的URL格式,會進(jìn)行站點(diǎn)URL的重寫,可以在webserver(Nginx)的配置中進(jìn)行rewrite,也可在在程序端進(jìn)行
以下使用Yaf框架進(jìn)行URL的重寫,進(jìn)行一些整理,方便日后查看
YAF的URL重寫方式主要有以下幾種,可以綜合使用
方式1:在配置文件中進(jìn)行
;a rewrite route match request /product/*/* routes.route_name.type="rewrite" routes.route_name.match="/product/:name/:value" routes.route_name.route.controller=product routes.route_name.route.action=info ;a regex route match request /list/*/* routes.route_name1.type="regex" routes.route_name1.match="#^list/([^/]*)/([^/]*)#" routes.route_name1.route.controller=Index routes.route_name1.route.action=action routes.route_name1.map.1=name routes.route_name1.map.2=value ;a simple route match /**?c=controller&a=action&m=module routes.route_name2.type="simple" routes.route_name2.controller=c routes.route_name2.module=m routes.route_name2.action=a ;a simple router match /**?r=PATH_INFO routes.route_name3.type="supervar" routes.route_name3.varname=r ;a map route match any request to controller routes.route_name4.type="map" routes.route_name4.controllerPrefer=TRUE routes.route_namer.delimiter="#!"
之后在Bootstrap.php中添加初始化函數(shù),函數(shù)名可按自己需求取,必需以_開頭才會被調(diào)用
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initConfig() {
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}
public function _initRoute(Yaf_Dispatcher $dispatcher) {
$router = $dispatcher->getRouter();
/**
* we can add some pre-defined routes in application.ini
*/
$router->addConfig(Yaf_Registry::get("config")->routes);
/**
* add a Rewrite route, then for a request uri:
* http://***/product/list/22/foo
* will be matched by this route, and result:
*
* [module] =>
* [controller] => product
* [action] => info
* [method] => GET
* [params:protected] => Array
* (
* [id] => 22
* [name] => foo
* )
*
*/
$route = new Yaf_Route_Rewrite(
"/product/list/:id/:name",
array(
"controller" => "product",
"action" => "info",
)
);
$router->addRoute('dummy', $route);
}
?>
方式2:直接在程序中,以數(shù)組方式配置
以下函數(shù)是放在Bootstrap.php中
public function _initRoute(Ap_Dispatcher $dispatcher) {
//在這里注冊自己的路由協(xié)議,默認(rèn)使用static路由
$router = Ap_Dispatcher::getInstance()->getRouter();
$routeConfig = array(
$router = Ap_Dispatcher::getInstance()->getRouter();
$routeConfig = array(
"item" => array(
"type" => "regex",
"match" => "#^/(software|game)/(.*).html$#",
"route" => array('action' => 'item'),
"map" => array( 1 => 'data_type', 2 => 'docid' ),
),
//正則匹配
"category" => array(
"type" => "regex",
"match" => "#^/(software|game|video)/(.*)/(list_(.*).html)?$#",
"route" => array('action' => 'list' ),
"map" => array( 1 => 'data_type', 2 => 'cid',4 => 'page_num' ),
),
//使用動態(tài)結(jié)果 :a 表示action
"name" => array(
"type" => "rewrite", //Yaf_Route_Rewrite route
"match" => "/user-list/:a/:id", //match only /user-list/開頭的
"route" => array(
'controller' => "user", //route to user controller,
'action' => ":a", //使用動態(tài)的action
),
),
);
$router->addConfig(new Ap_Config_Simple($routeConfig));
}
方式3:
/**
* Add a rewrite route to Yaf_Router route stack
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new Yaf_Route_rewrite(
"/product/:name/:id/*", //match request uri leading "/product"
array(
'controller' => "product", //route to product controller,
),
)
);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ThinkPHP、Zend?Framework2、Yaf、Laravel框架路由大比拼
- PHP擴(kuò)展框架之Yaf框架的安裝與使用
- PHP Yaf框架的簡單安裝使用教程(推薦)
- yii,CI,yaf框架+smarty模板使用方法
- 在PHP 7下安裝Swoole與Yar,Yaf的方法教程
- ubuntu14.04LTS安裝nginx+mariaDB+php7+YAF的方法
- 基于yaf框架和uploadify插件,做的一個導(dǎo)入excel文件,查看并保存數(shù)據(jù)的功能
- PHP7.1安裝yaf擴(kuò)展的方法
- Ubuntu上安裝yaf擴(kuò)展的方法
- 詳解Yaf框架PHPUnit集成測試方法
- Yaf框架封裝的MySQL數(shù)據(jù)庫操作示例
相關(guān)文章
在laravel中實(shí)現(xiàn)將查詢的對象轉(zhuǎn)換為多維數(shù)組的函數(shù)
今天小編就為大家分享一篇在laravel中實(shí)現(xiàn)將查詢的對象轉(zhuǎn)換為多維數(shù)組的函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Symfony頁面的基本創(chuàng)建實(shí)例詳解
這篇文章主要介紹了Symfony頁面的基本創(chuàng)建實(shí)例詳解,詳細(xì)分析了Symfony頁面的具體創(chuàng)建流程與注意事項(xiàng),需要的朋友可以參考下2015-01-01
PHP array_reduce()函數(shù)的應(yīng)用解析
這篇文章主要介紹了PHP array_reduce()的應(yīng)用,本文通過代碼舉例給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10
PHP重置數(shù)組為連續(xù)數(shù)字索引的幾種方式總結(jié)
下面小編就為大家分享一篇PHP重置數(shù)組為連續(xù)數(shù)字索引的幾種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
php基于curl擴(kuò)展制作跨平臺的restfule 接口
這篇文章主要介紹了php基于curl擴(kuò)展制作跨平臺的restfule 接口的相關(guān)資料以及詳細(xì)的代碼,有需要的小伙伴可以參考下。2015-05-05

