CakePHP 2: new exceptions

坚强是说给别人听的谎言 提交于 2019-12-04 12:18:06

问题


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

class SecurityException extends CakeException {};

Thanks!


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7690165/cakephp-2-new-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!