通常为了友好的URL格式,会进行站点URL的重写,可以在webserver(Nginx)的配置中进行rewrite,也可在在程序端进行
以下使用Yaf框架进行URL的重写,进行一些整理,方便日后查看
YAF的URL重写方式主要有以下几种,可以综合使用
方式1:在配置文件中进行
;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中添加初始化函数,函数名可按自己需求取,必需以_开头才会被调用
<?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:直接在程序中,以数组方式配置
以下函数是放在Bootstrap.php中
public function _initRoute(Ap_Dispatcher $dispatcher) {
//在这里注册自己的路由协议,默认使用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' ),
),
//使用动态结果 :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", //使用动态的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,
),
)
);
版权属于: 吕滔博客
原文地址: https://lvtao.net/dev/1571.html
转载时必须以链接形式注明原始出处及本声明。
[abc] |
匹配中括号中的单个字符,如a或b或c |
[^abc] |
匹配除了a、b、c等字符的其他单个字符 |
[a-z] |
匹配一个字符范围,如a到z |
[a-zA-Z] |
匹配一个字符范围,如a-z 或 A-Z |
^ |
匹配行的开始 |
$ |
匹配行的结束 |
\A |
匹配一个字符串的开始 |
\z |
匹配一个字符串的结束 |
. |
匹配任意单个字符 |
\s |
匹配空白字符,如空格,TAB |
\S |
匹配非空白字符 |
\d |
匹配一个数字 |
\D |
匹配非数字 |
\w |
匹配一个字母 |
\W |
匹配非字母 |
\b |
匹配字符边界 |
(...) |
引用所有括号中的内容 |
(a|b) |
a或者b |
a? |
零个或1个a |
a* |
零个或多个a |
a+ |
1个或多个a |
a{3} |
3次重复的a |
a{3,} |
3次或3次以上重复的a |
a{3,6} |
3到6次重复的a |
修正符 | |
/g |
查找所有可能的匹配 |
/i |
不区分大小写 |
/m |
多行匹配 |
/s |
单行匹配 |
/x |
忽略空白模式 |
/e |
可执行模式,PHP专有 |
/A |
强制从目标字符串开头匹配 |
/D |
使用$限制结尾字符,则不允许结尾有换行 |
/U |
只匹配最近的一个字符串;不重复匹配 |
添加新评论