<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/yaf.tutorials.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'yaf.tutorials.quickstart.php',
    1 => '快速入门',
    2 => '快速入门',
  ),
  'up' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => '示例',
  ),
  'prev' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => '示例',
  ),
  'next' => 
  array (
    0 => 'yaf.tutorials.routes.php',
    1 => '自定义路由',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/yaf/tutorials.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="yaf.tutorials.quickstart" class="section">
  <h2 class="title">快速入门</h2>

 <div class="example" id="example-1">
  <p><strong>示例 #1 一个典型的应用目录结构</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
- .htaccess           // 重写规则
+ public/
  | - index.php       // 应用入口
  | + css/
  | + js/
  | + img/
+ conf/
  | - application.ini // 应用配置
- application/
  - Bootstrap.php     // 引导
  + controllers/
     - Index.php      // 默认控制器
  + views/
     |+ index/
        - index.phtml // 默认动作的视图模板
  + library/          // 类库
  + models/           // 模型
  + plugins/          // 插件
</pre></div>
  </div>
  <div class="example-contents"><p>
   将 web 服务器的 <code class="literal">DocumentRoot</code> 设置为
   <var class="filename">public</var> 目录，这样从 web 就只能访问公开资源。
  </p></div>
 </div>

 <div class="example" id="example-2">
  <p><strong>示例 #2 多模块应用的目录结构</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
+ public/
+ conf/
+ application/
  + modules/
    + Index/       // 默认模块
      + controllers/
      + views/
    + Admin/       // 另一个模块
      + controllers/
      + views/
  + library/
  + models/
  + plugins/
  - Bootstrap.php
</pre></div>
  </div>
  <div class="example-contents"><p>
   在多模块应用中，每个模块在
   <code class="literal">application/modules/&lt;ModuleName&gt;/</code> 下都有自己的
   <code class="literal">controllers</code> 和 <code class="literal">views</code> 目录。
   已注册的模块在
   <a href="yaf.appconfig.php#configuration.yaf.modules" class="link">application.modules</a> 中声明。
  </p></div>
 </div>

 <div class="example" id="example-3">
  <p><strong>示例 #3 入口</strong></p>
  <div class="example-contents"><p>
   public 目录下的 <var class="filename">index.php</var> 是整个应用的唯一入口，
   应该把所有请求都重写到它（在 Apache + mod_php 下可以使用
   <var class="filename">.htaccess</var>，或者你的 web 服务器中的等效配置，见下文）。
  </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
define</span><span style="color: #007700">(</span><span style="color: #DD0000">"APPLICATION_PATH"</span><span style="color: #007700">,  </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)));

</span><span style="color: #0000BB">$app  </span><span style="color: #007700">= new </span><span style="color: #0000BB">Yaf_Application</span><span style="color: #007700">(</span><span style="color: #0000BB">APPLICATION_PATH </span><span style="color: #007700">. </span><span style="color: #DD0000">"/conf/application.ini"</span><span style="color: #007700">);
</span><span style="color: #0000BB">$app</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bootstrap</span><span style="color: #007700">() </span><span style="color: #FF8000">//call bootstrap methods defined in Bootstrap.php
 </span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">run</span><span style="color: #007700">();
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

 </div>

 <div class="example" id="example-4">
  <p><strong>示例 #4 重写规则</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
#for apache (.htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

#for nginx
server {
  listen 80;
  server_name domain.com;
  root   document_root;
  index  index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }
}

#for lighttpd
$HTTP[&quot;host&quot;] =~ &quot;(www.)?domain.com$&quot; {
  url.rewrite = (
     &quot;^/(.+)/?$&quot;  =&gt; &quot;/index.php/$1&quot;,
  )
}
</pre></div>
  </div>
 </div>

 <div class="example" id="example-5">
  <p><strong>示例 #5 引导</strong></p>
  <div class="example-contents"><p>
   <span class="classname"><strong class="classname">Bootstrap</strong></span> 类中所有名称以 <code class="literal">_init</code>
   开头的方法都会被 <span class="methodname"><a href="yaf-application.bootstrap.php" class="methodname">Yaf_Application::bootstrap()</a></span>
   按定义顺序自动调用。每个这样的方法都会收到
   <span class="classname"><a href="class.yaf-dispatcher.php" class="classname">Yaf_Dispatcher</a></span> 实例作为参数。
   名称为其他名称的方法不会被自动调用。
  </p></div>
  <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">Bootstrap </span><span style="color: #007700">extends </span><span style="color: #0000BB">Yaf_Bootstrap_Abstract
</span><span style="color: #007700">{
    public function </span><span style="color: #0000BB">_initConfig</span><span style="color: #007700">(</span><span style="color: #0000BB">Yaf_Dispatcher $dispatcher</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// called first
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">_initPlugin</span><span style="color: #007700">(</span><span style="color: #0000BB">Yaf_Dispatcher $dispatcher</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// called second
    </span><span style="color: #007700">}

    public function </span><span style="color: #0000BB">_initRoute</span><span style="color: #007700">(</span><span style="color: #0000BB">Yaf_Dispatcher $dispatcher</span><span style="color: #007700">)
    {
        </span><span style="color: #FF8000">// called third
    </span><span style="color: #007700">}
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
  </div>

 </div>

 <div class="example" id="example-6">
  <p><strong>示例 #6 应用配置</strong></p>
  <div class="example-contents"><p>
   <var class="filename">application.ini</var> 是应用的配置文件。
   其中可以使用 <var class="filename">index.php</var> 中定义的常量，
   并且各个分节之间可以相互继承。
  </p></div>
   <div class="example-contents">
<div class="inicode"><pre class="inicode">[yaf]
;APPLICATION_PATH is the constant defined in index.php
application.directory=APPLICATION_PATH &quot;/application/&quot;

;product section inherit from yaf section
[product:yaf]
foo=bar</pre>
</div>
   </div>

 </div>

 <div class="example" id="example-7">
  <p><strong>示例 #7 使用 PHP 数组作为应用配置</strong></p>
  <div class="example-contents"><p>
   除了 INI 配置文件，也可以把一个普通的 PHP 数组作为配置传递给
   <span class="methodname"><a href="yaf-application.construct.php" class="methodname">Yaf_Application::__construct()</a></span>。
  </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$config </span><span style="color: #007700">= array(
    </span><span style="color: #DD0000">"application" </span><span style="color: #007700">=&gt; array(
        </span><span style="color: #DD0000">"directory" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">APPLICATION_PATH </span><span style="color: #007700">. </span><span style="color: #DD0000">"/application/"</span><span style="color: #007700">,
    ),
);

</span><span style="color: #0000BB">$app </span><span style="color: #007700">= new </span><span style="color: #0000BB">Yaf_Application</span><span style="color: #007700">(</span><span style="color: #0000BB">$config</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

 </div>

 <div class="example" id="example-8">
  <p><strong>示例 #8 默认控制器</strong></p>
  <div class="example-contents"><p>
   在 Yaf 中，默认控制器名为 <span class="classname"><strong class="classname">IndexController</strong></span>：
  </p></div>
   <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">IndexController </span><span style="color: #007700">extends </span><span style="color: #0000BB">Yaf_Controller_Abstract
</span><span style="color: #007700">{
    </span><span style="color: #FF8000">// default action name
    </span><span style="color: #007700">public function </span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getView</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">content </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello World"</span><span style="color: #007700">;
    }
}
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

 </div>

 <div class="example" id="example-9">
  <p><strong>示例 #9 默认视图模板</strong></p>
  <div class="example-contents"><p>
   默认控制器/动作的视图脚本是
   <var class="filename">application/views/index/index.phtml</var>。Yaf 提供了一个
   名为 <span class="classname"><a href="class.yaf-view-simple.php" class="classname">Yaf_View_Simple</a></span> 的小型视图引擎，
   它的模板是普通的 PHP 脚本。
  </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000">&lt;html&gt;
 &lt;head&gt;
   &lt;title&gt;Hello World&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
   <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$content</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
</span> &lt;/body&gt;
&lt;/html&gt;</code></pre></div>
   </div>

 </div>

 <div class="example" id="example-10">
  <p><strong>示例 #10 运行应用</strong></p>
  <div class="example-contents"><p>
   将浏览器指向已配置的域名
   （例如 <code class="literal">http://www.example.com</code>），响应将如下所示：
  </p></div>
  <div class="example-contents"><p>以上示例的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
&lt;html&gt;
 &lt;head&gt;
   &lt;title&gt;Hello World&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
   hello world
 &lt;/body&gt;
&lt;/html&gt;
</pre></div>
  </div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="simpara">
    上面的最小示例也可以使用随
    <a href="https://github.com/laruence/yaf/tree/master/tools/cg" class="link external">&raquo;&nbsp;Yaf
    源码仓库</a> 一起发布的 Yaf 代码生成器
    （<var class="filename">tools/cg/yaf_cg</var>）生成：
   </p>
   <div class="example-contents screen">
<div class="cdata"><pre>
$ cd tools/cg
$ ./yaf_cg -d output_directory [-a application_name] [--namespace]
</pre></div>
   </div>
   <p class="simpara">
    <var class="filename">yaf_cg</var> 会生成比本教程所示更完整、带注释的骨架：
    一个带重写规则的 <var class="filename">public/</var> 入口、配置了插件和自定义路由的
    Bootstrap、示例模型、错误控制器以及视图模板。它是开发实际应用的
    一个良好起点。
   </p>
  </p></blockquote>
 </div>

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