雷林鹏分享:YAF路由问题

  这2天休年假,在家宅着学习研究了YAF框架,用YAF做过APP接口的项目,但是没有用来做过WEB方面的应用。趁着这2天在家想把博客用YAF进行一下改版,目的也想进一步学习一下YAF。

  在这过程中遇到不少问题,这里说下YAF路由的问题。

  现在博客的URL规则,使用的也是CI框架的regex路由规则:

  一级栏目:http://www.php230.com/category/

  二级栏目:http://www.php230.com/category/programming-language

  三级栏目:http://www.php230.com/category/web-server/nginx-server/

  文章内容页:http://www.php230.com/nginx-php-fastcgi-path_info.html

  现在通过YAF进行改版,也想保持现有的URL规则不变,所以只能通过YAF的Regex路由规则来实现。

  先来看看YAF手册上对YAF Regex路由规则的介绍:

  

  $route = new Yaf_Route_Regex(

  'product/([a-zA-Z-_0-9]+)',

  array(

  'controller' => 'products',

  'action' => 'view'

  ),

  array(

  //完成数字到字符变量的映射

  1 => 'ident'

  )

  );

  $router->addRoute('product', $route);

  参考资料:http://yaf.laruence.com/manual/yaf.routes.static.html#yaf.routes.regex

  按照手册上的介绍,自己写了相应的规则来对现有URL的实现,但是失败了,最后对规则做了些修改,代码如下:

  public function _initRoute(Yaf_Dispatcher $dispatcher) {

  $router = $dispatcher -> getRouter ();

  $route1 = new Yaf_Route_Regex('([a-zA-Z-_0-9]+.html)',array('controller' => 'content','action' => 'action'),array('1' => 'ident'));

  $router->addRoute('content', $route1);

  $route2 = new Yaf_Route_Regex('(category/[a-zA-Z-_0-9]+)',array('controller' => 'category','action' => 'topCategory'),array('1' => 'ident'));

  $router->addRoute('category1', $route2);

  $route3 = new Yaf_Route_Regex('(category/[a-zA-Z-_0-9]+/[a-zA-Z-_0-9]+/)',array('controller' => 'content','action' => 'subcat'),array('1' => 'ident'));

  $router->addRoute('category2', $route3);

  }

  通过上面的路由规则可以实现URL对控制器的关联,但是获取不到相应的ident变量的值,最后的解决方式是通过

  $_SERVER['REQUEST_URI']

  来处理,基本上可以解决现有的问题,但是总觉得哪里有问题,待会在慢慢研究...(编辑:雷林鹏 来源:网络 侵删)

posted @ 2019-04-29 13:36  lei林鹏  阅读(201)  评论(0编辑  收藏  举报