这块内容其实应该在入口文件中就应该说了,要不然大家没办法访问链接,不知道整个流程,怎么能边实践着,边敲代码呢。
项目中的入口文件index.php 是所有请求的入口, 一般都借助于rewrite规则, 把所有的请求都重定向到这个入口文件。若是不用rewrite规则,那么请使用基于query string的路由协议(Yaf_Route_Simple, Yaf_Route_Supervar),总之我们的目的就是把所有这个应用的请求, 都定向到上面提到的入口文件。
一、rewrite规则
这里只说nginx的定义,其余的请自行查找。
nginx.conf 配置文件
server { listen 80; server_name www.viphper.com; root document_root; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.php?$1 last; } } 配置后,我们便可以通过链接 http://www.viphper.com/index/index1 直接方法,index为controller,index1为action。
二、基于query string的路由协议
下面是常用的路由协议,根据自己需求定义。
Yaf_Route_Simple
Yaf_Route_Supervar
Yaf_Route_Static #默认路由协议
Yaf_Route_Map
Yaf_Route_Rewrite
Yaf_Route_Regex
1、默认路由
默认情况下,路由器是Yaf_Router,路由协议是Yaf_Route_Static,是基于HTTP路由的, 它期望一个请求是HTTP请求并且请求对象是使用Yaf_Request_Http。
Yaf_Router负责分析请求中的request uri,在去除掉base_uri以后,得出目标模板,控制器,动作,获取到真正的负载路由信息的request_uri片段, 具体的策略是, 根据”/”对(斜线)request_uri分段, 依次得到 Module,Controller,Action, 在得到Module以后, 还需要根据Yaf_Application::$modules来判断Module是否是合法的Module,如果不是,,则认为Module并没有体现在request_uri中, 而把原Module当做Controller, 原Controller当做Action。
看一个例子:
/** * 对于请求request_uri为"/ap/foo/bar/dummy/1" * base_uri为"/ap" * 则最后参加路由的request_uri为"/foo/bar/dummy/1" * 然后, 通过对URL分段, 得到如下分节 * foo, bar, dummy, 1 * 然后判断foo是不是一个合法的Module, 如果不是, 则认为结果如下: */ array( 'module' => '默认模块', 'controller' => 'foo', 'action' => 'bar', 'params' => array( 'dummy' => 1, ) ) /** * 而如果在配置文件中定义了ap.modules="Index,Foo", * 则此处就会认为foo是一个合法模块, 则结果如下 */ array( 'module' => 'foo', 'controller' => 'bar', 'action' => 'dummy', 'params' => array( 1 => NULL, ) )
如果在配置文件application.ini的内容中为:
[common] application.directory = APPLICATION_PATH"/application/" application.dispatcher.catchException = True ;出错的时候是否抛出异常 application.dispatcher.catchException = True ;是否使用默认的异常捕捉 application.cache_config = 1 application.view.ext = "html" application.modules = Index,User;声明存在的模块名 ;自定义默认的module controller action application.dispatcher.defaultModule = Index application.dispatcher.defaultController = Index application.dispatcher.defaultAction = index
下面以www.viphper.com为例说明:
www.viphper.com 会调用 /application/controllers/下面的Index.php中的indexAction,不过可以通过修改application.dispatcher.defaultAction来修改默认调用Action。
www.viphper.com/user/index/index (必须保证user模块存在)解析的时候 发现user数模块名称index是控制器名称 index是Action名称就会调用 application/modules/User/下面的Index.php
2、Yaf_Route_Simple 简单路由
自定义路由协议在 Bootstrap.php文件的 _initRoute中
public function _initRoute(Yaf_Dispatcher $dispatcher) { //在这里注册自己的路由协议,默认使用简单路由 $routeArr = new Yaf_Route_Simple('m','c','a'); $router->addRoute("name",$routeArr); }
访问地址: http://www.viphper.com/?m=user&c=account&a=add ( m 代表模块名称 c 代表控制器 a 代表Action; m、c、a是可以修改为任意的字符的)
得到的路由结果:
array( 'module' => 'user', 'controller' => 'account', 'action' => 'add', )
若访问:http://www.viphper.com/index.php?c=account&a=add
得到的路由结果:
array( 'module' => '默认模块', 'controller' => 'account', 'action' => 'add', )
3、Yaf_Route_Supervar
Yaf_Route_Supervar和Yaf_Route_Simple相似, 都是在query string中获取路由信息, 不同的是, 它获取的是一个类似包含整个路由信息的request_uri
public function __initRoute(Yaf_Dispatcher $dispatcher){ //Yaf_Route_Supervar 路由 $routeArr = new Yaf_Route_Supervar('r'); $router->addRoute("name",$routeArr); }
访问链接:http://www.viphper.com/?r=/user/account/index
将得到以下结果:
array( 'module' => 'user', 'controller' => 'account', 'action' => 'index', )
[fly-payread]
4、Yaf_Route_Rewrite 重写路由
//创建一个路由协议实例 $route = new Yaf_Route_Rewrite( 'product/:ident', array( 'controller' => 'products', 'action' => 'view' ) ); //使用路由器装载路由协议 $router->addRoute('product', $route);
访问链接:https://www.viphper.com/product/phone。
我们在路由协议Yaf_Route_Rewrite的构造函数中传递了两个变量,(‘product/:indent’)匹配路径, (array变量)是动作控制器;
在匹配路径我们使用冒号(:)和星号(*)来告诉路由协议如何匹配到路径中的每一个段。
冒号(:)是一个用于传递到动作控制器中的变量,’ident’是变量名,若访问http://www.viphper.com/product/phone
即表示我们创建一个变量名为ident,值是’phone’的变量,在动作控制器ProductsController/viewAction下获取到它的值:$this->getRequest()->getParam(‘ident’);
星号(*)的作用和冒号是一样的,不过星号表示的是Url之后的所有字段。假如路径为( ‘path/product/:ident/*’ ),访问链接为( http://www.viphper.com/product/phone/test/value1/another/value2 ),那么我们得到的结果为:
ident = phone test = value1 another = value2
需要注意的两点是:1)ident是可以自定义的,2)路由协议的行为,(变量名/值)是成对出现。
5、Yaf_Route_Regex 正则路由
其实正则路由和Yaf_Route_Rewrite差不多,你若是看懂了Yaf_Route_rewrite路由,那么理解起来就会很容易啦。
$route = new Yaf_Route_Regex( 'product/([a-zA-Z-_0-9]+)', array( 'controller' => 'products', 'action' => 'view' ) ); $router->addRoute('product', $route);
大家看例子应该可以直接看出来,实际就是直接将通配符换成正则表达式。
我们通过正则反向引用:$this->getRequest()->getParam(1) 来获取参数,当然这样会导致我们记不住是获取到第几个参数,
写过正则的估计都会有体会,不要害怕,鸟哥很贴心的给我们完成了变量的映射。
$route = new Yaf_Route_Regex( 'product/([a-zA-Z-_0-9]+)', array( 'controller' => 'products', 'action' => 'view' ), array( //完成数字到字符变量的映射 1 => 'ident' ) ); $router->addRoute('product', $route);
[/fly-payread]