CakeFest 2024: The Official CakePHP Conference

Yaf_Router クラス

(Yaf >=1.0.0)

はじめに

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]
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 には設定済みのデフォルトルート Yaf_Route_Static が用意されており、これは controller/action 形式の URI にマッチします。 さらに、モジュール名を最初のパス要素として指定できます。この場合の URI は module/controller/action 形式になります。 また、追加のパラメータを URI に追記できるようになっています。つまり controller/action/var1/value1/var2/value2 といった形式です。

注意:

モジュール名は設定ファイルで定義しておく必要があります。 たとえば application.module="Index,Foo,Bar" とすると、 index、foo、bar だけがモジュール名とみなされます。 設定をしなかった場合は、モジュール名とみなされるのは "Index" だけです。

ルートのマッチングの例を示します。

例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

クラス概要

class Yaf_Router {
/* プロパティ */
protected $_routes;
protected $_current;
/* メソッド */
public __construct()
public addRoute(string $name, Yaf_Route_Abstract $route): bool
public getRoutes(): mixed
public route(Yaf_Request_Abstract $request): bool
}

プロパティ

_routes

登録されたルートスタック。

_current

ルーティングを終えた後に、 今回のリクエストで使ったルートの名前がここに入ります。 この名前を取得するには Yaf_Router::getCurrentRoute() を使います。

目次

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top