Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2: new exceptions

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

class SecurityException extends CakeException {};

Thanks!

like image 902
entropid Avatar asked Oct 07 '11 16:10

entropid


2 Answers

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.

like image 116
luchomolina Avatar answered Sep 27 '22 01:09

luchomolina


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

like image 38
Mike T Avatar answered Sep 23 '22 01:09

Mike T