44
CakePHP2.0 ののののの ののののののののののの 2011.10.06 (Thur) のの の a.k.a. gusagi

20111006 cakephp2.0 study

  • Upload
    gusagi

  • View
    3.193

  • Download
    3

Embed Size (px)

DESCRIPTION

第2回CakePHP2.0勉強会「CakePHP2.0の例外処理 、ちょっとだけ食べてみた」

Citation preview

Page 1: 20111006 cakephp2.0 study

CakePHP2.0の例外処理ちょっとだけ食べてみた

2011.10.06 (Thur)橋口 誠 a.k.a. gusagi

Page 2: 20111006 cakephp2.0 study

自己紹介

Page 3: 20111006 cakephp2.0 study

@gusagi

menue 株式会社 所属• ケータイサイトとか作ってます

実は、今月は絶賛修羅場中… (´Д`;)

PHP 勉強会 @ 関東 の幹事やってます 『パーフェクト PHP 』書きました 実は CakePHP あまり使っていません…

Page 4: 20111006 cakephp2.0 study

今日のお題は

Page 5: 20111006 cakephp2.0 study

エラーハンドリング

Page 6: 20111006 cakephp2.0 study

アジェンダ1.3 までのエラーハンドリングについ

2.0 からのエラーハンドリングについて

手始めに PagesController でつまみ食い

独自 ExceptionRenderer について

Page 7: 20111006 cakephp2.0 study

1.3.x までは

Page 8: 20111006 cakephp2.0 study

Controller::cakeError()と

AppError

Page 9: 20111006 cakephp2.0 study

でも

Page 10: 20111006 cakephp2.0 study

cakeError removed

http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html

Page 11: 20111006 cakephp2.0 study

The error handling implementation has

dramatically changed in 2.0.

http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html

Page 12: 20111006 cakephp2.0 study

2.0 で実装が劇的に変わったよ!

Page 13: 20111006 cakephp2.0 study

2.0 からは

Page 14: 20111006 cakephp2.0 study

例外処理

Page 15: 20111006 cakephp2.0 study

Cakephp/lib/Cake/Error/

ErrorHandler.php ExceptionRenderer.php exceptions.php

Page 16: 20111006 cakephp2.0 study

app/Config/core.php

Configure::write( 'Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'ExceptionRenderer', 'log' => true));

Page 17: 20111006 cakephp2.0 study

Exceptions

Page 18: 20111006 cakephp2.0 study

lib/Cake/Error/exceptions.php

ForbiddenException NotFoundException InternalErrorException     ・     ・     ・ CakeException MissingControllerException MissingActionException MissingViewException

Page 19: 20111006 cakephp2.0 study

全部で 40 個

Page 20: 20111006 cakephp2.0 study

ということで?

Page 21: 20111006 cakephp2.0 study

例外処理をつまみ食い

Page 22: 20111006 cakephp2.0 study

手始めに

Page 23: 20111006 cakephp2.0 study

PagesControllerで

つまみ食い

Page 24: 20111006 cakephp2.0 study

PagesController::display()

Page 25: 20111006 cakephp2.0 study

MissingViewException

Page 26: 20111006 cakephp2.0 study

HTTP ステータスコード

Page 27: 20111006 cakephp2.0 study

“500 Internal Server Error”

Page 28: 20111006 cakephp2.0 study

普通は“ 404 Not Found”

じゃないの?

Page 29: 20111006 cakephp2.0 study

PagesController をちょっと修正

Page 30: 20111006 cakephp2.0 study

まずはlib/Cake/Controller/PagesController.php

↓app/Controller/PagesController.php

にコピー

Page 31: 20111006 cakephp2.0 study

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(); }}

Page 32: 20111006 cakephp2.0 study

Not Found になった!

Page 33: 20111006 cakephp2.0 study

でも画面表示が…

Page 34: 20111006 cakephp2.0 study

そんなときは独自 ExceptionRenderer

Page 35: 20111006 cakephp2.0 study

app/Config/core.php

Configure::write( 'Exception.renderer', 'CustomExceptionRenderer‘);App::uses('CustomExceptionRenderer', 'Error');

Page 36: 20111006 cakephp2.0 study

app/Lib/Error/CustomExceptionRenderer.php

を作成

※ ファイル・クラス名は任意で指定可能

Page 37: 20111006 cakephp2.0 study

app/Lib/Error/ClassName.php じゃないと

App::uses(‘ClassName’, ‘Error’);

で呼び出せないので気をつける

Page 38: 20111006 cakephp2.0 study

処理内容はお好みでどうぞ

Page 39: 20111006 cakephp2.0 study

たとえば、こんな感じで…

Page 40: 20111006 cakephp2.0 study

まとめ

Page 41: 20111006 cakephp2.0 study

CakePHP 2.0 ではエラーハンドリングが

柔軟にカスタマイズ可能!

Page 42: 20111006 cakephp2.0 study

マイグレーションはちょっと面倒かも知れないけど

Page 43: 20111006 cakephp2.0 study

例外処理を楽しんでね><

Page 44: 20111006 cakephp2.0 study

以上、ご清聴ありがとうございました