相思资源网 Design By www.200059.com

通常为了友好的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 /**"simple"
routes.route_name2.controller=c
routes.route_name2.module=m
routes.route_name2.action=a

;a simple router match /**"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中添加初始化函数,函数名可按自己需求取,必需以_开头才会被调用

<"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);
  }
"htmlcode">
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,
      ),
    )
  );

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Yaf框架路由重写,php,路由重写

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“详解php框架Yaf路由重写”

暂无详解php框架Yaf路由重写的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?