The Yaf_Router class

(Yaf >=1.0.0)

���

Yaf_Router�DZ�׼�Ŀ��·��.·����һ���õ�URI�˵㣨URL�е�URI��һ���֣�Ȼ��ֽ�����õ���һ��module, controller, ��action��Ҫ��������module, controller, ��action������һЩ�����IJ����Ǵ����һ��Yaf_Request_Abstract�Ķ����У�Ȼ��ͨ��Yaf_Dispatcher������ġ�·��ֻ����һ��:����ӵ��������ڵ�һ��controller�ַ�֮ǰ�� Yaf_Router �����������ʹ�ô�PHP�ṹ�����ƹ���ģ�����ת�����dz���ɢ�Ļ���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�ж��⸽�ӵIJ�����Ĭ����ʽ��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 addConfig ( Yaf_Config_Abstract $config ) : void
public addRoute ( string $name , Yaf_Route_Abstract $route ) : Yaf_Router
public__construct ( void )
public getCurrentRoute ( void ) : string
public getRoute ( string $name ) : void
public getRoutes ( void ) : void
public route ( Yaf_Request_Abstract $request ) : bool
}

����

_routes

_current

Table of Contents