<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="yaf.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="yaf.appconfig.html">Application Configuration</a></div> <div class="up"><a href="book.yaf.html">Yaf</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="yaf.tutorials" class="chapter"> <h1>Examples</h1> <div class="section"> <div class="example" id="example-4103"> <p><strong>Example #1 A classic Application directory layout</strong></p> <div class="example-contents screen"> <div class="cdata"><pre> - index.php - .htaccess + conf |- application.ini //application config - application/ - Bootstrap.php + controllers - Index.php //default controller + views |+ index - index.phtml //view template for default action + modules - library - models - plugins </pre></div> </div> </div> </div> <div class="section"> <div class="example" id="example-4104"> <p><strong>Example #2 Entry</strong></p> <div class="example-contents"><p>index.php in the top directory is the only way in of the application, you should rewrite all request to it(you can use .htaccess in Apache+php_mod) </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />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">__FILE__</span><span style="color: #007700">));<br /><br /></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">);<br /></span><span style="color: #0000BB">$app</span><span style="color: #007700">-></span><span style="color: #0000BB">bootstrap</span><span style="color: #007700">() </span><span style="color: #FF8000">//call bootstrap methods defined in Bootstrap.php<br /> </span><span style="color: #007700">-></span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="section"> <div class="example" id="example-4105"> <p><strong>Example #3 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 ****; server_name domain.com; root document_root; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.php/$1 last; } } #for lighttpd $HTTP["host"] =~ "(www.)?domain.com$" { url.rewrite = ( "^/(.+)/?$" => "/index.php/$1", ) } </pre></div> </div> </div> </div> <div class="section"> <div class="example" id="example-4106"> <p><strong>Example #4 Application config</strong></p> <div class="example-contents"> <div class="inicode"><pre class="inicode">[yaf] ;APPLICATION_PATH is the constant defined in index.php application.directory=APPLICATION_PATH "/application/" ;product section inherit from yaf section [product:yaf] foo=bar</pre> </div> </div> </div> </div> <div class="section"> <div class="example" id="example-4107"> <p><strong>Example #5 Default controller</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></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">{<br /> </span><span style="color: #FF8000">/* default action */<br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">indexAction</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_view</span><span style="color: #007700">-></span><span style="color: #0000BB">word </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello world"</span><span style="color: #007700">;<br /> </span><span style="color: #FF8000">//or<br /> // $this->getView()->word = "hello world";<br /> </span><span style="color: #007700">}<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="section"> <div class="example" id="example-4108"> <p><strong>Example #6 Default view template</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <html><br /> <head><br /> <title>Hello World</title><br /> </head><br /> <body><br /> <span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$word</span><span style="color: #007700">;</span><span style="color: #0000BB">?><br /></span> </body><br /></html></span> </code></div> </div> </div> </div> <div class="section"> <div class="example" id="example-4109"> <p><strong>Example #7 Run the Applicatioin</strong></p> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <html> <head> <title>Hello World</title> </head> <body> hello world </body> </html> </pre></div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> you can also generate above example by use Yaf codes generator, which could be found here yaf@github. </p> </p></blockquote> </div> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="yaf.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="yaf.appconfig.html">Application Configuration</a></div> <div class="up"><a href="book.yaf.html">Yaf</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>