Sunday, March 13, 2022

[FIXED] How to use custom exception from cakephp plugin?

Issue

I have some custom exception for my application. I placed them to app where it is running well. Now I want to move them to one of my plugin and use exception from their.

I have implemented custom exception as mentioned here: http://book.cakephp.org/2.0/en/development/exceptions.html

But, I am looking for same solution if I use exception classes from cakephp plugin.

Any suggestion...???


Solution

Lets say you want to put your exceptions in Custom plugin /app/Plugin/Custom/Lib folder:

// Location: /app/Plugin/Custom/Lib/CustomException.php
<?php
class CustomException extends CakeException {};

Now you need to load Custom plugin in /app/Config/bootstrap.php:

// Location: /app/Config/bootstrap.php
CakePlugin::load('Custom');

And wherever you want to use CustomException:

<?php
App::uses('CustomException', 'Custom.Lib');

class ApiController extends AppController {   

    public function demo() {
        throw new CustomException("Just testing");
    }

}


Answered By - Mindaugas Norvilas

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.