本篇主要介紹php框架Yaf路由重寫_php實例詳解,有興趣的朋友參考下,希望對大家有幫助。
通常為了友善的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="#!"
<?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); } ?>
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)); }
/** * 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, ), ) );
# #PHP7.1內部安裝
yaf擴充步驟詳解
#######Yaf框架安裝指南 ################# PHP7.1安裝###yaf###擴充的方法###########################以上是php框架Yaf路由重寫_php實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!