CakePHP 2: new exceptions

2019-03-20 13:17发布

问题:

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