11.14. The Yaf_Router class

简介

Yaf的路由器, 负责分析请求中的request uri, 得出目标模板, 控制器, 动作.

在PHP5.3之后, 打开yaf.use_namespace的情况下, 也可以使用 Yaf\Router

 
    final 
    Yaf_Router
    {
protected array _routes ;
protected string _current_route ;
public Yaf_Router addRoute ( string $name ,
Yaf_Route_Interface $route );

public boolean addConfig ( Yaf_Config_Abstract $routes_config );
public array getRoutes ( void );
public array getRoute ( string $name );
public string getCurrentRoute ( void );
public boolean route ( Yaf_Request_Abstract $request );
public boolean isModuleName ( string $name );
}
属性说明
_routes

路由器已有的路由协议栈, 默认的栈底总是名为"default"的Yaf_Route_Static路由协议的实例.

_current_route

在路由成功后, 路由生效的路由协议名

URL重写规则

Yaf的路由需要Web服务器把非静态资源的请求都转发到入口脚本, 以下是几种常见Web服务器的重写配置.

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]

Nginx:


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

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

Lighttpd:


url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)