通常为了友好的URL格式,会进行站点URL的重写,可以在webserver(Nginx)的配置中进行rewrite,也可在在程序端进行
以下使用Yaf框架进行URL的重写,进行一些整理,方便日后查看
YAF的URL重写方式主要有以下几种,可以综合使用
方式1:
在配置文件中进行;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中添加初始化函数,函数名可按自己需求取,必需以_开头才会被调用
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:直接在程序中,以数组方式配置
方式3: