国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

詳解php框架Yaf路由重寫

2024-05-04 23:58:10
字體:
供稿:網(wǎng)友

通常為了友好的URL格式,會(huì)進(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=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中添加初始化函數(shù),函數(shù)名可按自己需求取,必需以_開頭才會(huì)被調(diào)用

<?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:直接在程序中,以數(shù)組方式配置

以下函數(shù)是放在Bootstrap.php中

public function _initRoute(Ap_Dispatcher $dispatcher) {  //在這里注冊(cè)自己的路由協(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' ),    ),    //使用動(dòng)態(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", //使用動(dòng)態(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,      ),    )  );

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 九台市| 兴和县| 温宿县| 黔南| 新竹市| 奎屯市| 华池县| 瑞金市| 莲花县| 新邵县| 沾益县| 奎屯市| 托克逊县| 阿拉善左旗| 息烽县| 广灵县| 沂源县| 沅陵县| 镇巴县| 凤山市| 卢氏县| 石家庄市| 金湖县| 鹿邑县| 宜川县| 垦利县| 盐津县| 城步| 洪雅县| 秦安县| 榆林市| 县级市| 北安市| 本溪市| 霸州市| 常熟市| 昭觉县| 珲春市| 汾阳市| 离岛区| 平谷区|