RSS Feed

実験的に開発した小さなプログラムやWebサービスを公開。また、気になった最新IT技術情報をブログ形式でメモ。

zend_controller クイックスタート

MVCパターンでの開発を行えるようにするための準備とzend_controllerの動作確認。

Zend Frameworkは良い意味で自由度が高く、悪く言えばやさしくない所がありますね。(Railなフレームワークと比べて)

例えば今回実験するMVCパターンを利用するにはライブラリで機能は提供されていますが、自分で基本となるフォルダ構成やファイルを用意する必要があります。

ここではMVCパターンを利用した最初の 「Hello, World!」を出力するプログラムを実験します。

動作サンプル:こちら

実験ファイルダウンロード:zip.gifsample_data.zip

ディレクトリとファイルの作成

まずは、ディレクトリとファイルを用意します。
このレイアウトは、基本的なもので、利用する環境や要望によって変えることができます。

application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
html/
    .htaccess
    index.php

ドキュメントルートの設定

次に 先ほど作成したディレクトリ ”html”をドキュメントルートに設定します。

mod_rewrite ルールを用意する

作成した”html/.htaccess”にrewriteルールを下記のように設定します。
(下記はapache用のサンプルです)

[html/.htaccess]

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

起動ファイル(index.php)の作成

起動ファイルとはmod_rewriteで指定された実際にリクエストを受けるファイルです。

[html/index.php]

  1. < ?php
  2. require_once 'Zend/Controller/Front.php';
  3. Zend_Controller_Front::run('/path/to/app/controllers');

indexコントローラの作成

indexコントローラは他にコントローラの指定が無いときにディフォルトとして選択されるコントローラです。

コントローラとアクションとは、例えばURLが「http://www.sample.com/ctrl/act」の時、”ctrl”の部分がコントローラで”act”の部分がアクションになり、内部ではCtrlControllerクラスのactActionメソッド(CtrlController::actAction())をコールすることを意味しています。すなわちコントローラとはControllerクラスの名称でアクションはActionメソッドの名称ということになります。

アクションの指定が無いときのディフォルトは”indexAction”となっており、コントローラの指定が無いときはディフォルトは”IndexController”です。

[application/controllers/IndexController.php]

  1. < ?php
  2. /** Zend_Controller_Action */
  3. require_once 'Zend/Controller/Action.php';
  4.  
  5. class IndexController extends Zend_Controller_Action
  6. {
  7.     public function indexAction()
  8.     {
  9.     }
  10. }

ビュー(View)の作成

ViewRenderer は、コントローラ名 とアクション名 から処理するテンプレートを決めます。
デフォルトでは、コントローラと同一階層にある ”views”ディレクトリ内のディレクトリ”scripts/コントローラ名”にテンプレートが置かれ、テンプレートの拡張子は .phtml です。

すなわちアクション名が”index”アクション名が”index”の場合には ”application/views/scripts/index/index.phtml”がテンプレートになります。

[application/views/scripts/index/index.phtml]

  1. < !DOCTYPE html
  2. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html>
  5. <head>
  6.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7.   <title>My first Zend Framework App</title>
  8. </head>
  9. <body>
  10.     <h1>Hello, World!</h1>
  11. </body>
  12. </html>

エラー コントローラとビューの作成

デフォルトで、エラーハンドラプラグインが用意されており、エラー処理用のコントローラとそれに対応するビューを作成することで利用できます。 デフォルト設定では、デフォルトモジュールの ErrorController に errorAction というメソッドがあることを想定しています。

[application/controllers/ErrorController.php]

  1. < ?php
  2. /** Zend_Controller_Action */
  3. require_once 'Zend/Controller/Action.php';
  4.  
  5. class ErrorController extends Zend_Controller_Action
  6. {
  7.     public function errorAction()
  8.     {
  9.     }
  10. }

[application/views/scripts/error/error.phtml]

  1. < !DOCTYPE html
  2. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html>
  5. <head>
  6.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7.   <title>エラー</title>
  8. </head>
  9. <body>
  10.     <h1>エラーが発生しました</h1>
  11.     <p>エラーが発生しました。後ほどもう一度お試しください。</p>
  12. </body>
  13. </html>

以上で完了です。

正しく動作していれば、ドキュメントルートの設定で指定したURLにアクセスすることで「Hello, World!」が表示されます。
また、その状態で適当なディレクトリ(コントローラ)などの間違えたURLにアクセスするとエラーが表示されます。

関連記事