前回投稿したZend Frameworkを使ったサンプルで、例外関連の設定を試します。
エラーハンドラプラグインは、デフォルトの状態です。
次のようにアクションコントローラ内で例外をスローすると、「Zend Framework:Hello Worldを表示してみる」で作成したエラーページが表示されます。
BASEDIR/webapp03/modules/default/controllers/IndexController.php
-
class IndexController extends Tz_Controller_Action
-
{
-
// 略
-
-
public function throwAction()
-
{
-
throw new Exception('throwActionでスローします。');
-
}
-
// 略
-
}
以下、マニュアルにある方法の一部を試します。
独自の例外処理
エラーハンドラプラグインで例外を処理しない設定を試します。
フロントコントローラのthrowExceptionsを有効にすると、デフォルトのエラーページは表示されなくなります。
BASEDIR/webapp03/bootstrap.php
-
function bootstrap($webapp_dir)
-
{
-
// 2009/02/09 追加
-
require_once 'Zend/Controller/Action.php';
-
}
-
-
// 略
-
-
//require 'Zend/Controller/Front.php';// 2009/02/09 コメントアウト
-
$front = Zend_Controller_Front::getInstance();
-
-
$modules_dir = $webapp_dir . '/modules';
-
-
'default' => $modules_dir . '/default/controllers',
-
);
-
$front->setControllerDirectory($ctrl_dirs);
-
-
require_once $webapp_dir . '/MyAppPlugin.php';
-
$front->registerPlugin(new MyAppPlugin());
-
-
require_once $webapp_dir . '/lib/Tz/Controller/Action.php';
-
-
$front->throwExceptions(true);
-
-
try {
-
$front->dispatch();
-
}
-
catch (Exception $exception) {
-
//v($exception->getMessage());
-
$content = '<html><head>';
-
$content .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
-
$content .= '<title>エラー</title>';
-
$content .= '</head><body>';
-
$content .= 'エラーが発生しました.';
-
$content .= '</body></html>';
-
echo $content;
-
}
-
}
アクションコントローラ内で例外をスローすると、上記コードの32行目が実行されます。
例外時はデフォルトのコントローラとアクションを使用する
アクションコントローラで例外が発生した場合、デフォルトのコントローラとアクションを使用したページを表示するには、フロントコントローラのパラメータのuseDefaultControllerAlwaysをtrueに設定します。
-
$front->setParam('useDefaultControllerAlways', true);
この設定を行うとディスパッチャは例外をスローしないという記述がマニュアルにあります。
例外がスローされるほとんどの状況で、常にデフォルトのページが表示されることになります。
useDefaultControllerAlwaysとthrowExceptionsを同時に設定した場合は、throwExceptionsの設定は無視されるようです。
-
$front->setParam('useDefaultControllerAlways', true);
-
$front->throwExceptions(true);
アクションが存在しない時、デフォルトのアクションを使用する
コントローラ内で、アクションが存在しなかった場合、特定のページを表示するようにしてみます。
BASEDIR/webapp03/modules/default/controllers/CallController.php
-
class CallController extends Tz_Controller_Action
-
{
-
public function indexAction()
-
{
-
$this->view->testMsg = "テストメッセージです。" . $this->getRequest()->getControllerName();
-
}
-
-
public function __call($method, $args)
-
{
-
return $this->_redirect('/' . $this->getRequest()->getControllerName());
-
}
-
throw new Exception('Invalid method');
-
}
-
}
Callコントローラに存在しないアクションを指定すると、Callコントローラのindexアクションで処理されるページが表示されます。
存在するアクション内で例外をスローした場合は、__callメソッドは起動されませんので、エラーハンドラプラグイン等で処理することになります。
メモ
例外処理(エラーページの表示等)は、デフォルトのエラーハンドラプラグインで処理しようと思います。
- useDefaultControllerAlwaysは、たぶん今後使用しないので忘れる。
- throwExceptionsを有効にする方法は、使用する機会はないと思う。
- __callメソッドをオーバーライドする方法は、いつでも使えるように忘れない。
サンプルダウンロード
ダウンロードページにアーカイブしたファイルを置きました。
追記 2009/02/09
このページのサンプルは、Zend Framework 1.5.2を使用していました。
いつ頃からか、インクルードするZend Frameworkのライブラリが足りず、エラーがでるようになりました。
修正方法です。
1.
bootstrap.phpで定義したbootstrap()関数の最初に次のようなものを追加します。
-
require_once 'Zend/Controller/Action.php';
-
}
2.
次に、bootstrap.phpに定義したbootstrap()関数内の require 'Zend/Controller/Front.php'; をコメントアウトします。
-
//require 'Zend/Controller/Front.php';
以上で、Zend Framework 1.7.3では、必要なクラスをインクルードでき、「Fatal error: Class 'Zend_Controller_Action' not found 」というエラーがなくなります。