Upload
gusagi
View
3.193
Download
3
Embed Size (px)
DESCRIPTION
第2回CakePHP2.0勉強会「CakePHP2.0の例外処理、ちょっとだけ食べてみた」
Citation preview
CakePHP2.0の例外処理ちょっとだけ食べてみた
2011.10.06 (Thur)橋口 誠 a.k.a. gusagi
自己紹介
@gusagi
menue 株式会社 所属• ケータイサイトとか作ってます
実は、今月は絶賛修羅場中… (´Д`;)
PHP 勉強会 @ 関東 の幹事やってます 『パーフェクト PHP 』書きました 実は CakePHP あまり使っていません…
今日のお題は
エラーハンドリング
アジェンダ1.3 までのエラーハンドリングについ
て
2.0 からのエラーハンドリングについて
手始めに PagesController でつまみ食い
独自 ExceptionRenderer について
1.3.x までは
Controller::cakeError()と
AppError
でも
cakeError removed
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html
The error handling implementation has
dramatically changed in 2.0.
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html
2.0 で実装が劇的に変わったよ!
2.0 からは
例外処理
Cakephp/lib/Cake/Error/
ErrorHandler.php ExceptionRenderer.php exceptions.php
app/Config/core.php
Configure::write( 'Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'ExceptionRenderer', 'log' => true));
Exceptions
lib/Cake/Error/exceptions.php
ForbiddenException NotFoundException InternalErrorException ・ ・ ・ CakeException MissingControllerException MissingActionException MissingViewException
全部で 40 個
ということで?
例外処理をつまみ食い
手始めに
PagesControllerで
つまみ食い
PagesController::display()
MissingViewException
HTTP ステータスコード
“500 Internal Server Error”
普通は“ 404 Not Found”
じゃないの?
PagesController をちょっと修正
まずはlib/Cake/Controller/PagesController.php
↓app/Controller/PagesController.php
にコピー
app/Controller/PagesController.php//$this->render(implode('/', $path));try { $this->render(implode('/', $path));} catch (MissingViewException $exception) { if (Configure::read('debug') > 0) { $attributes = $exception->getAttributes(); throw new MissingViewException(array('file' => $attributes['file'])); } else { throw new NotFoundException(); }}
Not Found になった!
でも画面表示が…
そんなときは独自 ExceptionRenderer
app/Config/core.php
Configure::write( 'Exception.renderer', 'CustomExceptionRenderer‘);App::uses('CustomExceptionRenderer', 'Error');
app/Lib/Error/CustomExceptionRenderer.php
を作成
※ ファイル・クラス名は任意で指定可能
app/Lib/Error/ClassName.php じゃないと
App::uses(‘ClassName’, ‘Error’);
で呼び出せないので気をつける
処理内容はお好みでどうぞ
たとえば、こんな感じで…
まとめ
CakePHP 2.0 ではエラーハンドリングが
柔軟にカスタマイズ可能!
マイグレーションはちょっと面倒かも知れないけど
例外処理を楽しんでね><
以上、ご清聴ありがとうございました