Zend Frameworkを使って、Hello World!を表示するWebアプリケーションを作成してみます。

ディレクトリ構成

ディレクトリ構成は、だいたい次のようにしています。

BASEDIR/
    html/
        .htaccess
        index.php
    lib
        ZendFramework
        ZendFramework-1.0.1/
    webapp/
        modules/
            default/
                controllers/
                views/
                    scripts/

BASEDIRは、ドキュメントルートの一個上のディレクトリです。
htmlは、ドキュメントルートです。
lib/ZendFrameworkは、lib/ZendFramework-1.0.1へのシンボリックリンクにしています。

Zend Frameworkインストール

Zend Frameworkは、Zend Frameworkからダウンロードできます。
2007/08/25現在、1.0.1が最新安定版でした。

$ cd BASEDIR/lib
$ tar xvzf ZendFramework-1.0.1.tar.gz
$ ln -s ZendFramework-1.0.1 ZendFramework

ZendFramework-1.0.0の場合、解凍したユーザー以外読み込みできない状態となりましたので、次のように読み込み権限を追加しました。

$ find ZendFramework-1.0.0/ -type d -print0 | xargs -0 -t chmod 755
$ find ZendFramework-1.0.0/ -type f -print0 | xargs -0 -t chmod 644

あとは、php.ini.htaccess等で、BASEDIR/lib/ZendFramework/libraryをインクルードパスに含めます。
(絶対パスで、Zend FrameworkのPHPファイルをインクルードしてもよいです。)
サンプルでは、ソースコード内からインクルードパスを設定しています。

サンプル

Hello Worldを表示するサンプルコードのリストです。
.htaccessも含めて、7ファイルです。

BASEDIR/html/index.php
PHP:
  1. <?php
  2. $base_dir = dirname(dirname(__FILE__));
  3. $lib_dirpath = $base_dir . '/lib';
  4. $include_path = ini_get('include_path');
  5. ini_set('include_path',
  6.         $include_path . ':' . $lib_dirpath . '/ZendFramework/library' .
  7.         ':' . $lib_dirpath . '/smarty');
  8.  
  9. $webapp_dirpath = $base_dir . '/webapp01';
  10. require_once $webapp_dirpath . '/bootstrap.php';
  11.  
  12. bootstrap($webapp_dirpath);

BASEDIR/webapp01/bootstrap.php
PHP:
  1. function bootstrap($webapp_dir)
  2. {
  3.   require 'Zend/Controller/Front.php';
  4.   $front = Zend_Controller_Front::getInstance();
  5.  
  6.   $modules_dir = $webapp_dir . '/modules';
  7.  
  8.   $ctrl_dirs = array(
  9.                      'default' => $modules_dir . '/default/controllers',
  10.                      );
  11.   $front->setControllerDirectory($ctrl_dirs);
  12.  
  13.   try {
  14.     $front->dispatch();
  15.   }
  16.   catch (Exception $exception) {
  17.     v($exception);
  18.   }
  19. }

BASEDIR/webapp01/modules/default/controllers/IndexController.php
PHP:
  1. <?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4.   public function indexAction()
  5.   {
  6.     $this->view->testMsg = "テストメッセージです。";
  7.   }
  8. }

BASEDIR/webapp01/modules/default/controllers/ErrorController.php
PHP:
  1. <?php
  2. class ErrorController extends Zend_Controller_Action
  3. {
  4.   public function errorAction()
  5.   {
  6.   }
  7. }

BASEDIR/webapp01/modules/default/views/scripts/index/index.phtml
HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <title>Hello, World!</title>
  4. </head>
  5. Hello, World!<br />
  6. <?php echo $this->testMsg ?>
  7. </body>
  8. </html>

BASEDIR/webapp01/modules/default/views/scripts/error/error.phtml
HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <title>エラー</title>
  4. </head>
  5. <p>エラーが発生しました。</p>
  6. </body>
  7. </html>

その他、ドキュメントルートに.htaccessを置きます。
サンプルの.htacessは次のような内容にしています。

php_flag display_errors "On"

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

php_flag session.use_trans_sid off
php_flag session.use_only_cookies on
php_flag session.use_cookies "On"

php_value default_charset UTF-8
php_value mbstring.language Japanese
php_value mbstring.internal_encoding UTF-8
php_flag mbstring.encoding_translation OFF
php_value mbstring.http_input auto
php_value mbstring.http_output pass

php_value memory_limit 64M

サンプルダウンロード

ダウンロードページにアーカイブしたファイルを置きました。

Tags: ,

コメント / トラックバック 2件

  1. mono-blog より:

    ZendFrameworkを使ってみる。

    仕事でPHPを使うかも知れないので、せっかくなのでZendFrameworkをはじめました。
    こちらやこちらあたりを参考にサンプルを動かしてみました。
    DB周りのサンプルもさくっと動かしました。

  2. SYUs blog より:

    Zend Frameworkに・・・触れ損ねた

    z_developerのみではさまざまなPHPの機能を使えないので、Zend F

コメントをどうぞ