<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.yaf-route-interface.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'yaf-route-interface.route.php',
    1 => 'Yaf_Route_Interface::route',
    2 => '路由请求',
  ),
  'up' => 
  array (
    0 => 'class.yaf-route-interface.php',
    1 => 'Yaf_Route_Interface',
  ),
  'prev' => 
  array (
    0 => 'yaf-route-interface.assemble.php',
    1 => 'Yaf_Route_Interface::assemble',
  ),
  'next' => 
  array (
    0 => 'class.yaf-route-map.php',
    1 => 'Yaf_Route_Map',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/yaf/yaf_route_interface/route.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="yaf-route-interface.route" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Yaf_Route_Interface::route</h1>
  <p class="verinfo">(Yaf &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">Yaf_Route_Interface::route</span> &mdash; <span class="dc-title">路由请求</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-yaf-route-interface.route-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="modifier">function</span> <span class="methodname"><strong>Yaf_Route_Interface::route</strong></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>

  <p class="para rdfs-comment">
    <span class="methodname"><strong>Yaf_Route_Interface::route()</strong></span> 是用户自定义路由唯一需要实现的方法。
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="simpara">
      自 2.3.0 起，还应该实现另外一个方法，参阅 <span class="methodname"><a href="yaf-route-interface.assemble.php" class="methodname">Yaf_Route_Interface::assemble()</a></span>。
     </p>
    </p></blockquote>
  </p>
  <p class="para">
    如果此方法返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>，那么路由过程将会中止。否则，
    <span class="classname"><a href="class.yaf-router.php" class="classname">Yaf_Router</a></span> 将会调用路由栈中的下一个路由来路由请求。
  </p>

  <p class="para">
    此方法通过调用 <span class="methodname"><a href="yaf-request-abstract.setcontrollername.php" class="methodname">Yaf_Request_Abstract::setControllerName()</a></span>、
    <span class="methodname"><a href="yaf-request-abstract.setactionname.php" class="methodname">Yaf_Request_Abstract::setActionName()</a></span> 和
    <span class="methodname"><a href="yaf-request-abstract.setmodulename.php" class="methodname">Yaf_Request_Abstract::setModuleName()</a></span>
    将路由结果设置到参数 request 中。
   </p>

   <p class="para">
    这个方法最后还应该调用
    <span class="methodname"><a href="yaf-request-abstract.setrouted.php" class="methodname">Yaf_Request_Abstract::setRouted()</a></span> 将请求标记为已路由。
   </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-yaf-route-interface.route-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">request</code></dt>
    <dd>
     <p class="para">
       <span class="classname"><a href="class.yaf-request-abstract.php" class="classname">Yaf_Request_Abstract</a></span> 实例。
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-yaf-route-interface.route-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">

  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-yaf-route-interface.route-examples">
  <h3 class="title">示例</h3>
  <div class="example" id="example-1">
   <p><strong>示例 #1 <span class="methodname"><strong>Yaf_Route_Interface::route()</strong></span> 示例</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">class </span><span style="color: #0000BB">ApiRoute </span><span style="color: #007700">implements </span><span style="color: #0000BB">Yaf_Route_Interface </span><span style="color: #007700">{

    private </span><span style="color: #0000BB">$_prefix</span><span style="color: #007700">;

    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string $prefix</span><span style="color: #007700">) {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_prefix </span><span style="color: #007700">= </span><span style="color: #0000BB">$prefix</span><span style="color: #007700">;
    }

    public function </span><span style="color: #0000BB">route</span><span style="color: #007700">(</span><span style="color: #0000BB">Yaf_Request_Abstract $request</span><span style="color: #007700">): </span><span style="color: #0000BB">bool </span><span style="color: #007700">{
        </span><span style="color: #0000BB">$uri </span><span style="color: #007700">= </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getRequestUri</span><span style="color: #007700">();
        if (</span><span style="color: #0000BB">0 </span><span style="color: #007700">!== </span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$uri</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_prefix</span><span style="color: #007700">)) {
            </span><span style="color: #FF8000">/* 不归我们管，让路由器尝试下一个路由 */
            </span><span style="color: #007700">return </span><span style="color: #0000BB">false</span><span style="color: #007700">;
        }

        </span><span style="color: #FF8000">/* 把 URI "/api/user/list" 解析为 controller/action */
        </span><span style="color: #0000BB">$segments </span><span style="color: #007700">= </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">"/"</span><span style="color: #007700">, </span><span style="color: #0000BB">ltrim</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$uri</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_prefix</span><span style="color: #007700">)), </span><span style="color: #DD0000">"/"</span><span style="color: #007700">));
        </span><span style="color: #0000BB">$controller </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$segments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">ucfirst</span><span style="color: #007700">(</span><span style="color: #0000BB">$segments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]) : </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;
        </span><span style="color: #0000BB">$action </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$segments</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$segments</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] : </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;

        if (empty(</span><span style="color: #0000BB">$controller</span><span style="color: #007700">)) {
            return </span><span style="color: #0000BB">false</span><span style="color: #007700">;
        }

        </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setModuleName</span><span style="color: #007700">(</span><span style="color: #DD0000">"index"</span><span style="color: #007700">);
        </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setControllerName</span><span style="color: #007700">(</span><span style="color: #0000BB">$controller</span><span style="color: #007700">);
        </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setActionName</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">);
        </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setRouted</span><span style="color: #007700">(); </span><span style="color: #FF8000">/* 路由过程到此结束 */

        </span><span style="color: #007700">return </span><span style="color: #0000BB">true</span><span style="color: #007700">;
    }

    public function </span><span style="color: #0000BB">assemble</span><span style="color: #007700">(array </span><span style="color: #0000BB">$info</span><span style="color: #007700">, ?array </span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">): </span><span style="color: #0000BB">string </span><span style="color: #007700">{
        </span><span style="color: #0000BB">$uri </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_prefix </span><span style="color: #007700">. </span><span style="color: #DD0000">"/" </span><span style="color: #007700">. </span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">':c'</span><span style="color: #007700">]);
        if (isset(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">':a'</span><span style="color: #007700">])) {
            </span><span style="color: #0000BB">$uri </span><span style="color: #007700">.= </span><span style="color: #DD0000">"/" </span><span style="color: #007700">. </span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">':a'</span><span style="color: #007700">];
        }

        if (!empty(</span><span style="color: #0000BB">$query</span><span style="color: #007700">)) {
            </span><span style="color: #0000BB">$uri </span><span style="color: #007700">.= </span><span style="color: #DD0000">"?" </span><span style="color: #007700">. </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">);
        }

        return </span><span style="color: #0000BB">$uri</span><span style="color: #007700">;
    }
}

</span><span style="color: #FF8000">/**
 * 注册自定义路由
 */
</span><span style="color: #0000BB">Yaf_Dispatcher</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getRouter</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">addRoute</span><span style="color: #007700">(</span><span style="color: #DD0000">"api"</span><span style="color: #007700">, new </span><span style="color: #0000BB">ApiRoute</span><span style="color: #007700">(</span><span style="color: #DD0000">"/api"</span><span style="color: #007700">));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">/* http://yourdomain.com/api/user/list
 * 会路由为以下值：
 */
array(
  &quot;module&quot;     =&gt; &quot;index&quot;,
  &quot;controller&quot; =&gt; &quot;User&quot;,
  &quot;action&quot;     =&gt; &quot;list&quot;,
)</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-yaf-route-interface.route-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="yaf-route-interface.assemble.php" class="methodname" rel="rdfs-seeAlso">Yaf_Route_Interface::assemble()</a> - 组装一个请求</span></li>
   <li><span class="methodname"><a href="yaf-router.addroute.php" class="methodname" rel="rdfs-seeAlso">Yaf_Router::addRoute()</a> - 往 Router 中添加新的路由</span></li>
   <li><span class="methodname"><a href="yaf-request-abstract.setrouted.php" class="methodname" rel="rdfs-seeAlso">Yaf_Request_Abstract::setRouted()</a> - 将请求标记为已路由</span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>