<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.yaf.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'class.yaf-router.php',
    1 => 'Yaf_Router',
    2 => 'Yaf_Router クラス',
  ),
  'up' => 
  array (
    0 => 'book.yaf.php',
    1 => 'Yaf',
  ),
  'prev' => 
  array (
    0 => 'yaf-route-rewrite.route.php',
    1 => 'Yaf_Route_Rewrite::route',
  ),
  'next' => 
  array (
    0 => 'yaf-router.addconfig.php',
    1 => 'Yaf_Router::addConfig',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/yaf/yaf-router.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.yaf-router.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.yaf-router" class="reference">

 <h1 class="title">Yaf_Router クラス</h1>
 

 <div class="partintro"><p class="verinfo">(Yaf &gt;=1.0.0)</p>


  <div class="section" id="yaf-router.intro">
   <h2 class="title">はじめに</h2>
   <p class="para">
   <span class="classname"><strong class="classname">Yaf_Router</strong></span> は、フレームワークの標準のルーターです。
   ルーティングとは、URI エンドポイント (URI の中で、ベース URL の後に続く部分。
   <span class="methodname"><a href="yaf-request-abstract.setbaseuri.php" class="methodname">Yaf_Request_Abstract::setBaseUri()</a></span> を参照ください) を受け取ってそこからパラメータを抽出し、
   リクエストを受け取るモジュールやコントローラそしてアクションを判断する処理のことです。
   モジュール、コントローラ、アクション、そしてその他のパラメータは
   <span class="classname"><a href="class.yaf-request-abstract.php" class="classname">Yaf_Request_Abstract</a></span> オブジェクトにまとめられ、
   そして <span class="classname"><a href="class.yaf-dispatcher.php" class="classname">Yaf_Dispatcher</a></span> で処理します。
   ルーティングが行われるのは一度だけで、リクエストを最初に受け取ってから
   最初のコントローラにディスパッチする前に行われます。

   <span class="classname"><strong class="classname">Yaf_Router</strong></span> は、mod_rewrite 風の機能を
   PHP を使って実現できるような設計になっています。
   Ruby on Rails のルーティング方式を参考にしており、
   ウェブサーバーの URL リライト機能に関する事前知識は不要です。
   Apache の場合は、次のような mod_rewrite ルールを書けば使えます。
   <div class="example" id="example-1">
    <p><strong>例1 Apache 用のリライトルール</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php</pre>
</div>
   </div>

   </div>
   あるいは、次のようにもできます (こちらのほうを推奨します)。
   <div class="example" id="example-2">
    <p><strong>例2 Apache 用のリライトルール</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]</pre>
</div>
   </div>

  </div>
   Lighttpd の場合は、次のようなリライトルールを指定します。
   <div class="example" id="example-3">
    <p><strong>例3 Lighttpd 用のリライトルール</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">url.rewrite-once = (
  &quot;.*\?(.*)$&quot; =&gt; &quot;/index.php?$1&quot;,
  &quot;.*\.(js|ico|gif|jpg|png|css|html)$&quot; =&gt; &quot;$0&quot;,
  &quot;&quot; =&gt; &quot;/index.php&quot;
)</pre>
</div>
   </div>

  </div>
   Nginx の場合は、次のようなリライトルールを指定します。
   <div class="example" id="example-4">
    <p><strong>例4 Nginx 用のリライトルール</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}</pre>
</div>
   </div>

  </div>
   </p>
  </div>


  <div class="section" id="yaf-router.default">
   <h2 class="title">デフォルトのルート</h2>
   <p class="para">
   <span class="classname"><strong class="classname">Yaf_Router</strong></span> には設定済みのデフォルトルート <span class="classname"><a href="class.yaf-route-static.php" class="classname">Yaf_Route_Static</a></span> が用意されており、これは
   controller/action 形式の URI にマッチします。
   さらに、モジュール名を最初のパス要素として指定できます。この場合の URI は
   module/controller/action 形式になります。
   また、追加のパラメータを URI に追記できるようになっています。つまり
   controller/action/var1/value1/var2/value2 といった形式です。
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      モジュール名は設定ファイルで定義しておく必要があります。
      たとえば application.module=&quot;Index,Foo,Bar&quot; とすると、
      index、foo、bar だけがモジュール名とみなされます。
      設定をしなかった場合は、モジュール名とみなされるのは &quot;Index&quot; だけです。
     </p>
    </p></blockquote>
   </p>

   <p class="para">
   ルートのマッチングの例を示します。
   <div class="example" id="example-5">
    <p><strong>例5 <span class="classname"><a href="class.yaf-route-static.php" class="classname">Yaf_Route_Static</a></span> のデフォルトルート</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">// このように設定しているものとします
$conf = array(
   &quot;application&quot; =&gt; array(
      &quot;modules&quot; =&gt; &quot;Index,Blog&quot;,
   ),
);

コントローラのみ
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</pre>
</div>
   </div>

  </div>

  </p>
  </div>

  <div class="section" id="yaf-router.synopsis">
   <h2 class="title">クラス概要</h2>


   <div class="classsynopsis">
    <span class="ooclass"><strong class="classname"></strong></span>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Yaf_Router</strong>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* プロパティ */</div>
    <div class="fieldsynopsis">
     <span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-router.php#yaf-router.props.routes">$<var class="varname">_routes</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-router.php#yaf-router.props.current">$<var class="varname">_current</var></a></var>;</div>


    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* メソッド */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="yaf-router.construct.php" class="methodname">__construct</a></span>()</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.addconfig.php" class="methodname">addConfig</a></span>(<span class="methodparam"><span class="type"><a href="class.yaf-config-abstract.php" class="type Yaf_Config_Abstract">Yaf_Config_Abstract</a></span> <code class="parameter">$config</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.addroute.php" class="methodname">addRoute</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type">Yaf_Route_Abstract</span> <code class="parameter">$route</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getcurrentroute.php" class="methodname">getCurrentRoute</a></span>(): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getroute.php" class="methodname">getRoute</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="class.yaf-route-interface.php" class="type Yaf_Route_Interface">Yaf_Route_Interface</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getroutes.php" class="methodname">getRoutes</a></span>(): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.route.php" class="methodname">route</a></span>(<span class="methodparam"><span class="type"><a href="class.yaf-request-abstract.php" class="type Yaf_Request_Abstract">Yaf_Request_Abstract</a></span> <code class="parameter">$request</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

   }</div>


  </div>

  

  <div class="section" id="yaf-router.props">
   <h2 class="title">プロパティ</h2>
   <dl>
    
     <dt id="yaf-router.props.routes"><var class="varname">_routes</var></dt>
     <dd>
      <p class="para">
       登録されたルートスタック。
      </p>
     </dd>
    
    
     <dt id="yaf-router.props.current"><var class="varname">_current</var></dt>
     <dd>
      <p class="para">
       ルーティングを終えた後に、
       今回のリクエストで使ったルートの名前がここに入ります。

       この名前を取得するには
       <span class="methodname"><a href="yaf-router.getcurrentroute.php" class="methodname">Yaf_Router::getCurrentRoute()</a></span> を使います。
      </p>
     </dd>
    
   </dl>
  </div>



 </div>

 
























































<h2>目次</h2><ul class="chunklist chunklist_reference"><li><a href="yaf-router.addconfig.php">Yaf_Router::addConfig</a> — 設定で定義したルートをルーターに追加する</li><li><a href="yaf-router.addroute.php">Yaf_Router::addRoute</a> — 新しいルートをルーターに追加する</li><li><a href="yaf-router.construct.php">Yaf_Router::__construct</a> — Yaf_Router のコンストラクタ</li><li><a href="yaf-router.getcurrentroute.php">Yaf_Router::getCurrentRoute</a> — 使われているルート名を取得する</li><li><a href="yaf-router.getroute.php">Yaf_Router::getRoute</a> — 名前を指定してルートを取得する</li><li><a href="yaf-router.getroutes.php">Yaf_Router::getRoutes</a> — 登録済みのルートを取得する</li><li><a href="yaf-router.route.php">Yaf_Router::route</a> — The route purpose</li></ul>
</div>
<?php manual_footer($setup); ?>