例1 標準的なアプリケーションのディレクトリ構造

- index.php 
- .htaccess 
+ conf
  |- application.ini // アプリケーションの設定
- application/
  - Bootstrap.php   
  + controllers
     - Index.php // デフォルトのコントローラ
  + views    
     |+ index   
        - index.phtml // デフォルトアクション用のビューテンプレート
  + modules 
  - library
  - models  
  - plugins 

例2 エントリ

トップディレクトリの index.php が、アプリケーションの唯一の入り口です。 index.php へのすべてのリクエストをリダイレクトしなければいけません (Apache+mod_php なら .htaccess を使えます)。

<?php
define
("APPLICATION_PATH", dirname(__FILE__));

$app = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
$app->bootstrap() // Bootstrap.php で定義した bootstrap メソッドを呼びます
->run();
?>

例3 リライトルール

# apache (.htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

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

# lighttpd
$HTTP["host"] =~ "(www.)?domain.com$" {
  url.rewrite = (
     "^/(.+)/?$"  => "/index.php/$1",
  )
}

例4 アプリケーションの設定

[yaf]
; APPLICATION_PATH は index.php で定義した定数
application.directory=APPLICATION_PATH "/application/" 

; product セクションは yaf セクションを継承する
[product:yaf]
foo=bar

例5 デフォルトのコントローラ

<?php
class IndexController extends Yaf_Controller_Abstract {
/* デフォルトアクション */
public function indexAction() {
$this->_view->word = "hello world";
// あるいは
// $this->getView()->word = "hello world";
}
}
?>

例6 デフォルトのビューテンプレート

<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo $word;?>
</body>
</html>

例7 アプリケーションの実行結果

上の例の出力は、 たとえば以下のようになります。

<html>
 <head>
   <title>Hello World</title>
 </head>
 <body>
   hello world
 </body>
</html>

注意:

このサンプルは Yaf のコードジェネレータで生成できます。コードジェネレータは yaf@github にあります。