您好,欢迎访问一九零五行业门户网

对于php框架中Yaf路由重写的解析

这篇文章主要介绍了详解php框架yaf路由重写,内容挺不错的,现在分享给大家,也给大家做个参考。
通常为了友好的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=productroutes.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=indexroutes.route_name1.route.action=actionroutes.route_name1.map.1=nameroutes.route_name1.map.2=value;a simple route match /**?c=controller&a=action&m=moduleroutes.route_name2.type="simple"routes.route_name2.controller=croutes.route_name2.module=mroutes.route_name2.action=a;a simple router match /**?r=path_inforoutes.route_name3.type="supervar"routes.route_name3.varname=r;a map route match any request to controllerroutes.route_name4.type="map"routes.route_name4.controllerprefer=trueroutes.route_namer.delimiter="#!"
之后在bootstrap.php中添加初始化函数,函数名可按自己需求取,必需以_开头才会被调用
<?phpclass 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, ), ) );
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
关于thinkphp5.0框架中独立配置与动态配置的方法
以上就是对于php框架中yaf路由重写的解析的详细内容。
其它类似信息

推荐信息