CakePHP 2: new exceptions

2019-03-20 13:41发布

I'd like to create a new exception, called SecurityException. Where should I put the code?

class SecurityException extends CakeException {};

Thanks!

2条回答
该账号已被封号
2楼-- · 2019-03-20 13:46

I followed luchomolina's 2nd answer (commented on his own answer), and thought it deserved to be an official answer:

Here's another approach: "put exceptions in ([plugin-if-any])/Lib/Error/Exception/NameOfTheException.php and use App::uses('NameOfTheException', 'Error/Exception') where they're needed. Seemed like a Cake'ish way to do it, and they're not included unless one is actually thrown." –luchomolina

查看更多
萌系小妹纸
3楼-- · 2019-03-20 14:08

Create an exceptions.php file, put it on the Lib folder and fill it up with all your *Exception classes. Then include it on your application's bootstrap file.

require APP . 'Lib' . DS . 'exceptions.php';

All exceptions will become available application wide.

查看更多
登录 后发表回答