首页 > 后端开发 > php教程 > php框架Yaf路由重写实例代码

php框架Yaf路由重写实例代码

小云云
发布: 2023-03-20 09:56:01
原创
2337 人浏览过

通常为了友好的URL格式,会进行站点URL的重写,可以在webserver(Nginx)的配置中进行rewrite,也可在在程序端进行,本文主要和大家介绍php框架Yaf路由重写,给大家做个参考,希望能帮助到大家。

以下使用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="#!"
登录后复制

之后在Bootstrap.php中添加初始化函数,函数名可按自己需求取,必需以_开头才会被调用


<?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(&#39;dummy&#39;, $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(&#39;action&#39; => &#39;item&#39;),
      "map" => array( 1 => &#39;data_type&#39;, 2 => &#39;docid&#39; ),
    ),
    //正则匹配
    "category" => array(
      "type" => "regex",
      "match" => "#^/(software|game|video)/(.*)/(list_(.*).html)?$#",
      "route" => array(&#39;action&#39; => &#39;list&#39; ),
      "map" => array( 1 => &#39;data_type&#39;, 2 => &#39;cid&#39;,4 => &#39;page_num&#39; ),
    ),
    //使用动态结果 :a 表示action
    "name" => array(
      "type" => "rewrite",    //Yaf_Route_Rewrite route
      "match" => "/user-list/:a/:id", //match only /user-list/开头的
      "route" => array(
        &#39;controller&#39; => "user", //route to user controller,
        &#39;action&#39;   => ":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(
        &#39;controller&#39; => "product", //route to product controller,
      ),
    )
  );
登录后复制

相关推荐:

yaf路由配置规则_PHP教程

以上是php框架Yaf路由重写实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板