yaf 路由协议配置测试

好久没写博客了,最近老板让我转到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/abc
routes.regex.type="regex"
routes.regex.match="#^list/([^/]*)/([^/]*)#"
routes.regex.route.controller=Index
routes.regex.route.action=action
routes.regex.map.1=name
routes.regex.map.2=value

;index.php?m=m&c=c&a=a&test=555--->index.php/m/c/a/test/555
routes.simple.type="simple"
routes.simple.module=m
routes.simple.controller=c
routes.simple.action=a

;index.php?r=m/c/a&test=555--->index.php/m/c/a/test/555
routes.supervar.type="supervar"
routes.supervar.varname=r
;默认的Yaf_Application
;将会读取此节配置

;补充路由定义
;index.php/product/test/555--->index.php/index/product/info/name/test/value/555
routes.rewrite.type="rewrite"
;routes.rewrite.match="/product/:name/:value"
routes.rewrite.match="/product/:ident/*"
routes.rewrite.route.module=user
routes.rewrite.route.controller=index
routes.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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值