<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'yaf.tutorials.quickstart.php',
    1 => 'Quick Start',
    2 => 'Quick Start',
  ),
  'up' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => 'Examples',
  ),
  'prev' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => 'Examples',
  ),
  'next' => 
  array (
    0 => 'yaf.tutorials.routes.php',
    1 => 'Custom Routes',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Quick Start</h2>

 <div class="example" id="example-1">
  <p><strong>Example #1 A classic Application directory layout</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
- .htaccess           // Rewrite rules
+ public/
  | - index.php       // Application entry
  | + css/
  | + js/
  | + img/
+ conf/
  | - application.ini // Application config
- application/
  - Bootstrap.php     // Bootstrap
  + controllers/
     - Index.php      // Default controller
  + views/
     |+ index/
        - index.phtml // View template for default action
  + library/          // Libraries
  + models/           // Models
  + plugins/          // Plugins
</pre></div>
  </div>
  <div class="example-contents"><p>
   Set the web server <code class="literal">DocumentRoot</code> to the
   <var class="filename">public</var> directory, so that only the public
   assets are accessible from the web.
  </p></div>
 </div>

 <div class="example" id="example-2">
  <p><strong>Example #2 A multi-module Application directory layout</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
+ public/
+ conf/
+ application/
  + modules/
    + Index/       //default module
      + controllers/
      + views/
    + Admin/       //another module
      + controllers/
      + views/
  + library/
  + models/
  + plugins/
  - Bootstrap.php
</pre></div>
  </div>
  <div class="example-contents"><p>
   In a multi-module application, each module has its own
   <code class="literal">controllers</code> and <code class="literal">views</code> directory
   under <code class="literal">application/modules/&lt;ModuleName&gt;/</code>. The
   registered modules are declared in
   <a href="yaf.appconfig.php#configuration.yaf.modules" class="link">application.modules</a>.
  </p></div>
 </div>

 <div class="example" id="example-3">
  <p><strong>Example #3 Entry</strong></p>
  <div class="example-contents"><p>
   <var class="filename">index.php</var> in the public directory is the only way
   into the application, you should rewrite all requests to it (you can
   use <var class="filename">.htaccess</var> in Apache + mod_php, or the
   equivalent in your web server, see below).
  </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>Example #4 Rewrite rule</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>Example #5 Bootstrap</strong></p>
  <div class="example-contents"><p>
   Any method of the <span class="classname"><strong class="classname">Bootstrap</strong></span> class whose name
   begins with <code class="literal">_init</code> is automatically called, in
   definition order, by
   <span class="methodname"><a href="yaf-application.bootstrap.php" class="methodname">Yaf_Application::bootstrap()</a></span>. Each such method
   receives the <span class="classname"><a href="class.yaf-dispatcher.php" class="classname">Yaf_Dispatcher</a></span> instance as its
   argument. Methods with other names are not called automatically.
  </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>Example #6 Application config</strong></p>
  <div class="example-contents"><p>
   <var class="filename">application.ini</var> is the application configuration
   file. Constants defined in <var class="filename">index.php</var> can be used
   in it, and sections can inherit from one another.
  </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>Example #7 Application config as a PHP array</strong></p>
  <div class="example-contents"><p>
   Besides an INI configuration file, a plain PHP array can also be passed
   to <span class="methodname"><a href="yaf-application.construct.php" class="methodname">Yaf_Application::__construct()</a></span> as the
   configuration.
  </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>Example #8 Default controller</strong></p>
  <div class="example-contents"><p>
   In Yaf, the default controller is named
   <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>Example #9 Default view template</strong></p>
  <div class="example-contents"><p>
   The view script for the default controller/action is
   <var class="filename">application/views/index/index.phtml</var>. Yaf provides
   a small view engine called <span class="classname"><a href="class.yaf-view-simple.php" class="classname">Yaf_View_Simple</a></span>,
   whose templates are plain PHP scripts.
  </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>Example #10 Run the Application</strong></p>
  <div class="example-contents"><p>
   Point the browser at the configured domain
   (e.g. <code class="literal">http://www.example.com</code>) and the response
   will look like:
  </p></div>
  <div class="example-contents"><p>The above example will output
something similar to:</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">Note</strong>: 
   <p class="simpara">
    The minimal example above can also be generated with the Yaf code
    generator (<var class="filename">tools/cg/yaf_cg</var>), which ships with
    the <a href="https://github.com/laruence/yaf/tree/master/tools/cg" class="link external">&raquo;&nbsp;Yaf
    source repository</a>:
   </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> generates a more complete, commented
    skeleton than the one shown in this tutorial: a
    <var class="filename">public/</var> entry with rewrite rules, a Bootstrap
    with configuration, plugin and custom-route setup, an example
    model, an error controller and view templates. It is a good
    starting point for a real application.
   </p>
  </p></blockquote>
 </div>

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