對於php架構中Yaf路由重寫的解析

來源:互聯網
上載者:User
這篇文章主要介紹了詳解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,      ),    )  );

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.