(Yaf >=1.0.0)
Yaf_Router是標準的框架路由.路由是一個(gè)拿到URI端點(diǎn)(URL中的URI的一部分)然后分解參數得到哪一個(gè)module, controller, 和action需要接受請求。module, controller, 和action,還有一些其他的參數是打包在一個(gè)Yaf_Request_Abstract的對象中,然后通過(guò)Yaf_Dispatcher來(lái)處理的。路由只發(fā)生一次:最初接到請求并且在第一個(gè)controller分發(fā)之前。 Yaf_Router 是設計來(lái)允許使用純PHP結構的類(lèi)似功能模塊的跳轉。它非常松散的基于Ruby on Rails的路由,并且不需要你提前就知道webserver URL跳轉的相關(guān)知識。它是設計來(lái)處理一個(gè)Apache 跳轉模塊的規則(一個(gè)) Yaf_Router是設計來(lái)允許mod_rewrite
示例 #1 Apache的跳轉規則
RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
示例 #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]
示例 #3 Lighttpd的跳轉規則
url.rewrite-once = ( ".*\?(.*)$" => "/index.php?$1", ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0", "" => "/index.php" )
示例 #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預設了一個(gè)默認路由,它將以controller/action的形式匹配URIs。此外,一個(gè)module的名字可以被指定為第一路徑元素,允許URIs設置為module/controller/action的形式。最后,它也會(huì )匹配一些URI中額外附加的參數,默認形式是controller/action/var1/value1/var2/value2。
注意:
Module的名字必須要定義在配置中,就application.module="Index,Foo,Bar"而言,在這種情況下,僅僅index, foo 和 bar能被考慮作為為一個(gè)module的名稱(chēng)。如果沒(méi)有在配置文件中定義,那么Yaf使用默認的module名字“Index”。
如何匹配這些路由的一些例子:
示例 #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