エラー処理を適当に作る
Zend Framework: 1.11.0
PHP:5.3.3
エラー発生時
Zend_Exceptionを投げておしまい。
throw new Zend_Exception('エラーメッセージ',エラーコード)
エラーハンドリング
エラーハンドリングにはErrorController.phpを使用する。
この場合、Zend_Controller_Plugin_ErrorHandlerを通って呼び出される。
特に_handleErrorは一度目を通しておきたい。
肝心のコードの書き方は下記のとおり。
application/controllers/ErrorController.php
class ErrorController extends Zend_Controller_Action { public function errorAction() { $errors = $this->_getParam('error_handler'); // 省略 switch ($errors->type) { // 省略 default: /* * * $errors->exception は PHP の Exception * 使用可能なメソッド * public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] ) * final public string getMessage ( void ) * final public Exception getPrevious ( void ) * final public int getCode ( void ) * final public string getFile ( void ) * final public int getLine ( void ) * final public array getTrace ( void ) * final public string getTraceAsString ( void ) * public string __toString ( void ) */ // ここで条件分岐する $exception = $errors->exception; switch ($exception->getCode()) { case エラーコード: $this->getResponse()->setHttpResponseCode(403); $this->view->message = '権限がありません'; break: default: // application error $this->getResponse()->setHttpResponseCode(500); $this->view->message = 'Application error'; break; } } // 省略 }