Yaf
PHP Manual

The Yaf_Router class

(Yaf >=1.0.0)

简介

Yaf_Router是标准的框架路由.路由是一个拿到URI端点(URL中的URI的一部分)然后分解参数得到哪一个module, controller, 和action需要接受请求。module, controller, 和action,还有一些其他的参数是打包在一个Yaf_Request_Abstract的对象中,然后通过Yaf_Dispatcher来处理的。路由只发生一次:最初接到请求并且在第一个controller分发之前。 Yaf_Router 是设计来允许使用纯PHP结构的类似功能模块的跳转。它非常松散的基于Ruby on Rails的路由,并且不需要你提前就知道webserver URL跳转的相关知识。它是设计来处理一个Apache 跳转模块的规则(一个) Yaf_Router是设计来允许mod_rewrite

Example #1 Apache的跳转规则

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
or (preferred):

Example #2 Apache的跳转规则

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
如果使用Lighttpd,下面的跳转规则是有效的:

Example #3 Lighttpd的跳转规则

url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)
如果使用Nginx,下面的跳转规则是有效的:

Example #4 Nginx的跳转规则

server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}

默认路由

Yaf_Router预设了一个默认路由,它将以controller/action的形式匹配URIs。此外,一个module的名字可以被指定为第一路径元素,允许URIs设置为module/controller/action的形式。最后,它也会匹配一些URI中额外附加的参数,默认形式是controller/action/var1/value1/var2/value2。

Note:

Module的名字必须要定义在配置中,就application.module="Index,Foo,Bar"而言,在这种情况下,仅仅index, foo 和 bar能被考虑作为为一个module的名称。如果没有在配置文件中定义,那么Yaf使用默认的module名字“Index”。

如何匹配这些路由的一些例子:

Example #5 Yaf_Route_Static(default route)example

// Assuming the following configure:
$conf = array(
   "application" => array(
      "modules" => "Index,Blog",
   ),
);

Controller only:
http://example/news
    controller == news
Action only(when defined yaf.action_prefer=1 in php.ini)
    action  == news
 
Invalid module maps to controller name:
http://example/foo
    controller == foo
 
Module + controller:
http://example/blog/archive
    module     == blog
    controller == archive
 
Module + controller + action:
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list
 
Module + controller + action + params:
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc

类摘要

Yaf_Router {
/* 属性 */
protected $_routes ;
protected $_current ;
/* 方法 */
public void addConfig ( Yaf_Config_Abstract $config )
public Yaf_Router addRoute ( string $name , Yaf_Route_Abstract $route )
public__construct ( void )
public string getCurrentRoute ( void )
public void getRoute ( string $name )
public void getRoutes ( void )
public bool route ( Yaf_Request_Abstract $request )
}

属性

_routes

_current

Table of Contents


Yaf
PHP Manual