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();
    }
}

Read 4 comments


  1. Well the title is a little misleading. Its not actually disabling the plugin, its removing user access to it. “Disabling” sounds more like removing it from the helpers array.

    Either way, setting the allowed plugins array should probably be done outside of the function, other wise its not very dynamic is it?


  2. using cakephp1.3, i’m actually doing this…

    function __construct() {
    if (Configure::read(‘debug’)) {
    $plugins = App::objects(‘plugin’);
    if (in_array(‘DebugKit’,$plugins)) {
    $this->components[] = ‘DebugKit.Toolbar';
    }
    }
    parent::__construct();
    }

  3. Pingback: on Planet CakePHP « Afzet Inc.

Leave a Reply