Site icon Leonid Mamchenkov

Disable and enable CakePHP plugins on the fly

I am currently working on a rather large project which is based on CakePHP framework.  In order to simplify the task, I’ve broken down the whole application into a number of CakePHP plugins.  Now, however, I want to enable/disable plugins on the fly.  After a brief search around I couldn’t find how to do that.  Asking a question at #cakephp IRC channel did it.  RabidFire instantly replied with the link that gave me an idea.   30 seconds later I had a working solution.

CakePHP plugins extend AppController.  So all that one needs to do is add the following lines to app/app_controller.php (Using CakePHP 2.0, but it’s trivial to adopt for earlier versions):

public function beforeFilter() {
    $allowedPlugins = array('crm', 'articles');
    if (!empty($this->request->params['plugin']) && !in_array($this->request->params['plugin'], $allowedPlugins)) {
        throw new ForbiddenException();
    }
}
Exit mobile version