|
Yaf_Router クラスはじめにYaf_Router は、フレームワークの標準のルーターです。 ルーティングとは、URI エンドポイント (URI の中で、ベース URL の後に続く部分。 Yaf_Request_Abstract::setBaseUri を参照ください) を受け取ってそこからパラメータを抽出し、 リクエストを受け取るモジュールやコントローラそしてアクションを判断する処理のことです。 モジュール、コントローラ、アクション、そしてその他のパラメータは Yaf_Request_Abstract オブジェクトにまとめられ、 そして Yaf_Dispatcher で処理します。 ルーティングが行われるのは一度だけで、リクエストを最初に受け取ってから 最初のコントローラにディスパッチする前に行われます。 Yaf_Router は、mod_rewrite 風の機能を PHP を使って実現できるような設計になっています。 Ruby on Rails のルーティング方式を参考にしており、 ウェブサーバーの URL リライト機能に関する事前知識は不要です。 Apache の場合は、次のような 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 には設定済みのデフォルトルート Yaf_Route_Static が用意されており、これは controller/action 形式の URI にマッチします。 さらに、モジュール名を最初のパス要素として指定できます。この場合の URI は module/controller/action 形式になります。 また、追加のパラメータを URI に追記できるようになっています。つまり controller/action/var1/value1/var2/value2 といった形式です。
ルートのマッチングの例を示します。 例5 Yaf_Route_Static のデフォルトルート // このように設定しているものとします
$conf = array(
"application" => array(
"modules" => "Index,Blog",
),
);
コントローラのみ
http://example/news
controller == news
アクションのみ (php.ini で yaf.action_prefer=1 とした場合)
action == news
モジュール名として無効な場合はコントローラ名とみなします
http://example/foo
controller == foo
モジュール + コントローラ
http://example/blog/archive
module == blog
controller == archive
モジュール + コントローラ + アクション
http://example/blog/archive/list
module == blog
controller == archive
action == list
モジュール + コントローラ + アクション + パラメータ
http://example/blog/archive/list/sort/alpha/date/desc
module == blog
controller == archive
action == list
sort == alpha
date == desc
クラス概要Yaf_Router
class Yaf_Router
{
/* プロパティ */
protected
$_routes;
protected
$_current;
/* メソッド */
public __construct()
public bool addConfig(Yaf_Config_Abstract
$config)public bool addRoute(string
$name, Yaf_Route_Abstract $route)public string getCurrentRoute()
public Yaf_Route_Interface getRoute(string
$name)public mixed getRoutes()
public bool route(Yaf_Request_Abstract
}$request)プロパティ
|