yaf路由

yaf有6种路由模式

————Yaf_Route_Static,Yaf_Route_Simple,Yaf_Route_Supervar,Yaf_Route_Map,Yaf_Route_Rewrite,Yaf_Route_Regex

添加路由模式有两种方法1)是直接写在bootstrap.php的_initRoute方法中,2)是写在application.ini配置中,然后在bootstarp.php的initRoute方法中加载

Yaf_Route_Static模式,该模式也是默认的

这种其实是通过pathinfo来进行路由的
例如 lovehyh.top/Index/Index/index/name/huyouheng/age/23 ;

simple 简单路由

1)直接在bootstrap.php的_initRoute方法中写

public function _initRoute(Yaf\Dispatcher $dispatcher) {  
    $router = $dispatcher->getInstance()->getRouter(); //得到路由实例
    $simple = new \Yaf\Route\Simple('m','c','a'); //对应的分别是模块 控制器 方法
    $router->addRoute($simple); //添加路由规则
}

这样便可以通过 ?m=Index&c=Index&a=index 访问
2)在配置文件中写 ,然后在bootstrap的__initRoute中添加
application.ini文件中

[routes]
 routes.simple.type = 'simple' //路由的模式
 routes.simple.moudel = 'Index'  //模型
 routes.simple.controller = 'Index' //控制器
 routes.simple.action = 'index' //方法

[product : common : routes] // 添加配置对儿到这里,否则不会生效

bootstrap.php的_initRoute中

$router = $dispatcher->getInstance()->getRouter() ;//依然是得到路由实例
$router->addConfig(Yaf\Registry::get('config')->routes); //添加配置文件的路由

Yaf_Route_Regex 正则路由

也就是对url进行正则匹配

 routes.user.type = 'regex'
 routex.user.match = '#^/user/([0-9]+)[\/]?$#'  // /user/101
 routes.user.route.module = 'User'
 routes.user.route.controller = 'User' 
 routes.user.route.action = 'show'
 routes.user.map.1 = userId //将匹配到的数字给userId

这样便可以通过 /user/101 进行访问

Yaf_Route_Rewrite

routes.post.type = 'rewrite'
routes.post.match = '/post/:id'
routes.post.route.module = Post
routes.post.route.controller = Index
routes.post.route.action = show

Yaf_Route_Map

路由和参数分离

Yaf_Route_Supervar

这就是结合了 Simple 和 默认路由(statix)
可以通过 127.0.0.1/?t=User/Index/index 访问

在bootstrap的__initRoute中添加

        $route = new \Yaf\Route\Supervar('t');
        $router->addRoute('myRoute', $route);

当然所有的路由都要自己去定义,路由过多的话,相对其他框架是有点繁琐的.

你可能感兴趣的:(yaf路由)