<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'yaf.tutorials.quickstart.php',
    1 => 'D&eacute;marrage rapide',
    2 => 'D&eacute;marrage rapide',
  ),
  'up' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => 'Exemples',
  ),
  'prev' => 
  array (
    0 => 'yaf.tutorials.php',
    1 => 'Exemples',
  ),
  'next' => 
  array (
    0 => 'yaf.tutorials.routes.php',
    1 => 'Routes personnalis&eacute;es',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Démarrage rapide</h2>

 <div class="example" id="example-1">
  <p><strong>Exemple #1 Une arborescence d&#039;application classique</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
- .htaccess           // Règles de réécriture
+ public/
  | - index.php       // Point d&#039;entrée de l&#039;application
  | + css/
  | + js/
  | + img/
+ conf/
  | - application.ini // Configuration de l&#039;application
- application/
  - Bootstrap.php     // Amorçage
  + controllers/
     - Index.php      // Contrôleur par défaut
  + views/
     |+ index/
        - index.phtml // Modèle de vue pour l&#039;action par défaut
  + library/          // Bibliothèques
  + models/           // Modèles
  + plugins/          // Plugins
</pre></div>
  </div>
  <div class="example-contents"><p>
   Il faut définir le <code class="literal">DocumentRoot</code> du serveur web sur le
   répertoire <var class="filename">public</var>, de sorte que seules les
   ressources publiques soient accessibles depuis le web.
  </p></div>
 </div>

 <div class="example" id="example-2">
  <p><strong>Exemple #2 Une arborescence d&#039;application multi-modules</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
+ public/
+ conf/
+ application/
  + modules/
    + Index/       //module par défaut
      + controllers/
      + views/
    + Admin/       //un autre module
      + controllers/
      + views/
  + library/
  + models/
  + plugins/
  - Bootstrap.php
</pre></div>
  </div>
  <div class="example-contents"><p>
   Dans une application multi-modules, chaque module possède ses propres
   répertoires <code class="literal">controllers</code> et <code class="literal">views</code>
   sous <code class="literal">application/modules/&lt;NomDuModule&gt;/</code>. Les
   modules enregistrés sont déclarés dans
   <a href="yaf.appconfig.php#configuration.yaf.modules" class="link">application.modules</a>.
  </p></div>
 </div>

 <div class="example" id="example-3">
  <p><strong>Exemple #3 Point d&#039;entrée</strong></p>
  <div class="example-contents"><p>
   Le fichier <var class="filename">index.php</var> situé dans le répertoire public
   est le seul point d&#039;entrée de l&#039;application ; il faut y réécrire toutes
   les requêtes (il est possible d&#039;utiliser un fichier
   <var class="filename">.htaccess</var> avec Apache + mod_php, ou l&#039;équivalent
   pour le serveur web utilisé, voir ci-dessous).
  </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">//appelle les méthodes d'amorçage définies dans 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>Exemple #4 Règle de réécriture</strong></p>
  <div class="example-contents screen">
<div class="cdata"><pre>
#pour apache (.htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

#pour 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;
  }
}

#pour 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>Exemple #5 Amorçage</strong></p>
  <div class="example-contents"><p>
   Toute méthode de la classe <span class="classname"><strong class="classname">Bootstrap</strong></span> dont le nom
   commence par <code class="literal">_init</code> est automatiquement appelée, dans
   l&#039;ordre de définition, par
   <span class="methodname"><a href="yaf-application.bootstrap.php" class="methodname">Yaf_Application::bootstrap()</a></span>. Chacune de ces
   méthodes reçoit l&#039;instance de <span class="classname"><a href="class.yaf-dispatcher.php" class="classname">Yaf_Dispatcher</a></span> en
   argument. Les méthodes portant un autre nom ne sont pas appelées
   automatiquement.
  </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">// appelée en premier
    </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">// appelée en deuxième
    </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">// appelée en troisième
    </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>Exemple #6 Configuration de l&#039;application</strong></p>
  <div class="example-contents"><p>
   <var class="filename">application.ini</var> est le fichier de configuration de
   l&#039;application. Les constantes définies dans
   <var class="filename">index.php</var> peuvent y être utilisées, et les sections
   peuvent hériter les unes des autres.
  </p></div>
   <div class="example-contents">
<div class="inicode"><pre class="inicode">[yaf]
;APPLICATION_PATH est la constante définie dans index.php
application.directory=APPLICATION_PATH &quot;/application/&quot;

;la section product hérite de la section yaf
[product:yaf]
foo=bar</pre>
</div>
   </div>

 </div>

 <div class="example" id="example-7">
  <p><strong>Exemple #7 Configuration de l&#039;application sous forme de tableau PHP</strong></p>
  <div class="example-contents"><p>
   Outre un fichier de configuration INI, il est également possible de
   passer un simple tableau PHP à
   <span class="methodname"><a href="yaf-application.construct.php" class="methodname">Yaf_Application::__construct()</a></span> en guise de
   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>Exemple #8 Contrôleur par défaut</strong></p>
  <div class="example-contents"><p>
   Dans Yaf, le contrôleur par défaut se nomme
   <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">// nom de l'action par défaut
    </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>Exemple #9 Modèle de vue par défaut</strong></p>
  <div class="example-contents"><p>
   Le script de vue du contrôleur/action par défaut est
   <var class="filename">application/views/index/index.phtml</var>. Yaf fournit un
   petit moteur de vue nommé <span class="classname"><a href="class.yaf-view-simple.php" class="classname">Yaf_View_Simple</a></span>, dont
   les modèles sont de simples scripts 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>Exemple #10 Exécution de l&#039;application</strong></p>
  <div class="example-contents"><p>
   En pointant le navigateur sur le domaine configuré
   (par exemple <code class="literal">http://www.example.com</code>), la réponse
   ressemblera à :
  </p></div>
  <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus est similaire à :</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">
    L&#039;exemple minimal ci-dessus peut également être généré avec le
    générateur de code de Yaf (<var class="filename">tools/cg/yaf_cg</var>),
    livré avec le <a href="https://github.com/laruence/yaf/tree/master/tools/cg" class="link external">&raquo;&nbsp;dépôt
    source de Yaf</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> génère un squelette plus complet et
    commenté que celui présenté dans ce tutoriel : un point d&#039;entrée
    <var class="filename">public/</var> avec ses règles de réécriture, un
    Bootstrap avec la configuration, la mise en place des plugins et des
    routes personnalisées, un modèle d&#039;exemple, un contrôleur d&#039;erreur et
    des modèles de vue. C&#039;est un bon point de départ pour une véritable
    application.
   </p>
  </p></blockquote>
 </div>

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