yaf 路由协议配置测试

来源:互联网 发布:网络接口图片 编辑:程序博客网 时间:2024/04/30 05:04

好久没写博客了,最近老板让我转到php部门,要学习Yaf,它的路由机制真心难理解,主要是官方文档没有写清楚,网上也没有太多的实用的文档。特地写一篇博客供大家参考。各种路由协议的原理请大家参考http://www.laruence.com/manual/yaf.routes.static.html#N20845,这里不再重复,直接说用法。

注:假设我们的主机名为http://www.domain.com

默认路由协议

在使用这个协议时一定要在url中写上index.php,否则会出现404,这与你是否配置默认主页与否无关,例如:http://www.domain.com/index.php/user/index/index

对于其它路由协议我推荐在application.ini中配置好,下面是我的配置文件:

;自定义路由;顺序很重要;index.php/list/test/abc--->index.php/index/index/index/name/test/value/abcroutes.regex.type="regex"routes.regex.match="#^list/([^/]*)/([^/]*)#"routes.regex.route.controller=Indexroutes.regex.route.action=actionroutes.regex.map.1=nameroutes.regex.map.2=value;index.php?m=m&c=c&a=a&test=555--->index.php/m/c/a/test/555routes.simple.type="simple"routes.simple.module=mroutes.simple.controller=croutes.simple.action=a;index.php?r=m/c/a&test=555--->index.php/m/c/a/test/555routes.supervar.type="supervar"routes.supervar.varname=r;默认的Yaf_Application;将会读取此节配置;补充路由定义;index.php/product/test/555--->index.php/index/product/info/name/test/value/555routes.rewrite.type="rewrite";routes.rewrite.match="/product/:name/:value"routes.rewrite.match="/product/:ident/*"routes.rewrite.route.module=userroutes.rewrite.route.controller=indexroutes.rewrite.route.action=index

这样我们就可以在Bootstrap.php中这样写了:

<?php   class Bootstrap extends Yaf_Bootstrap_Abstract {        public function _initConfig(Yaf_Dispatcher $dispatcher) {                      //配置文件添加          Yaf_Registry::set('config', Yaf_Application::app()->getConfig());        }        //配置路由        public function _initRoute(Yaf_Dispatcher $dispatcher) {          $router = Yaf_Dispatcher::getInstance()->getRouter();          $router->addConfig(Yaf_Registry::get("config")->routes);        }   }?>

Yaf_Route_SimpleYaf_Route_Supervar的用法官网说得很清楚,这里也不再重复

重点谈一下Yaf_Route_Rewrite

这个路由协议的配置见上面ini中“补充路由定义”,routes.rewrite.type="rewrite"指出是该协议是哪个路由协议(Yaf_Route_Rewrite),routes.rewrite.match="/product/:ident/*"指出了uri的模式,routes.rewrite.route.module=user、routes.rewrite.route.controller=index、routes.rewrite.route.action=index分别指出了module,controller,action,所以请求的uri一旦符合对应的模式,就会访问..../modules/User/controllers/index.php/IndexAction

Yaf_Route_Regex同Yaf_Route_Rewrite

好了,时间不早了,大家如果还有什么疑问欢迎给我发私信或者发邮箱或者给我评论~

0 0