The Yaf_Router class

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

示例 #1 Apache的跳轉規則

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

示例 #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,下面的跳轉規則是有效的:

示例 #3 Lighttpd的跳轉規則

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

示例 #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。

注意:

Module的名字必須要定義在配置中,就application.module="Index,Foo,Bar"而言,在這種情況下,僅僅index, foo 和 bar能被考慮作為為一個module的名稱。如果沒有在配置檔案中定義,那麼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

類摘要

class 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()
public getCurrentRoute(): string
public getRoute(string $name): void
public getRoutes(): void
public route(Yaf_Request_Abstract $request): bool
}

屬性

_routes

_current

目錄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *