実験的に開発した小さなプログラムやWebサービスを公開。また、気になった最新IT技術情報をブログ形式でメモ。
zend_controller クイックスタート
MVCパターンでの開発を行えるようにするための準備とzend_controllerの動作確認。
Zend Frameworkは良い意味で自由度が高く、悪く言えばやさしくない所がありますね。(Railなフレームワークと比べて)
例えば今回実験するMVCパターンを利用するにはライブラリで機能は提供されていますが、自分で基本となるフォルダ構成やファイルを用意する必要があります。
ここではMVCパターンを利用した最初の 「Hello, World!」を出力するプログラムを実験します。
動作サンプル:こちら
実験ファイルダウンロード:
sample_data.zip
ディレクトリとファイルの作成
まずは、ディレクトリとファイルを用意します。
このレイアウトは、基本的なもので、利用する環境や要望によって変えることができます。
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
html/
.htaccess
index.php
ドキュメントルートの設定
次に 先ほど作成したディレクトリ ”html”をドキュメントルートに設定します。
mod_rewrite ルールを用意する
作成した”html/.htaccess”にrewriteルールを下記のように設定します。
(下記はapache用のサンプルです)
[html/.htaccess]
- RewriteEngine on
- RewriteRule !.(js|ico|gif|jpg|png|css)$ index.php
起動ファイル(index.php)の作成
起動ファイルとはmod_rewriteで指定された実際にリクエストを受けるファイルです。
[html/index.php]
- < ?php
- require_once 'Zend/Controller/Front.php';
- 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]
- < ?php
- /** Zend_Controller_Action */
- require_once 'Zend/Controller/Action.php';
- class IndexController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- }
- }
ビュー(View)の作成
ViewRenderer は、コントローラ名 とアクション名 から処理するテンプレートを決めます。
デフォルトでは、コントローラと同一階層にある ”views”ディレクトリ内のディレクトリ”scripts/コントローラ名”にテンプレートが置かれ、テンプレートの拡張子は .phtml です。
すなわちアクション名が”index”アクション名が”index”の場合には ”application/views/scripts/index/index.phtml”がテンプレートになります。
[application/views/scripts/index/index.phtml]
- < !DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>My first Zend Framework App</title>
- </head>
- <body>
- <h1>Hello, World!</h1>
- </body>
- </html>
エラー コントローラとビューの作成
デフォルトで、エラーハンドラプラグインが用意されており、エラー処理用のコントローラとそれに対応するビューを作成することで利用できます。 デフォルト設定では、デフォルトモジュールの ErrorController に errorAction というメソッドがあることを想定しています。
[application/controllers/ErrorController.php]
- < ?php
- /** Zend_Controller_Action */
- require_once 'Zend/Controller/Action.php';
- class ErrorController extends Zend_Controller_Action
- {
- public function errorAction()
- {
- }
- }
[application/views/scripts/error/error.phtml]
- < !DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>エラー</title>
- </head>
- <body>
- <h1>エラーが発生しました</h1>
- <p>エラーが発生しました。後ほどもう一度お試しください。</p>
- </body>
- </html>
以上で完了です。
正しく動作していれば、ドキュメントルートの設定で指定したURLにアクセスすることで「Hello, World!」が表示されます。
また、その状態で適当なディレクトリ(コントローラ)などの間違えたURLにアクセスするとエラーが表示されます。

RSS Feed