0

I'm attempting to update a site to CakePHP 2.5 from 2.4 but for some reason it is ignoring the routes from my plugins. I've figured out that CakePlugin::loadAll needs updating to the following:-

CakePlugin::loadAll(array(array(
    'MyPlugin' => array(
        'routes' => true
    )
)));

However, it ignores the routes from MyPlugin (they don't appear to get loaded at all. I've got CakePlugin::routes() in my app/Config/routes.php file.

I've taken a look inside CakePlugin and CakePlugin::$_plugins seems to be setting MyPlugin['routes'] to false.

Can anyone shed any light on what's wrong here?

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
  • I don't think you want to load all routes for all plugins. you will get an error for each plugin that does not have the route file. why not simply load the route on a per plugin basis rather than using :loadALL ? – Angel S. Moreno Jul 17 '14 at 17:50
  • @AngelS.Moreno the above is meant to load the routes on a per plugin basis and has always worked in the past for us. That's why the plugin has a "'routes' => true" parameter. It's possible to use loadAll to include all plugin routes where they exist without getting errors (which incidentally fixes my issue, although isn't ideal): CakePlugin::loadAll(array( array('routes' => true, 'ignoreMissing' => true), 'MyPlugin' )); – drmonkeyninja Jul 18 '14 at 08:56
  • true. I just don't see the `'ignoreMissing' => true` param in your example. Could it be that there is a plugin with no route and it makes all the subsequent routes to not be included? you mentioned that adding the param `'ignoreMissing' => true` fixes your issue. why is that not ideal? – Angel S. Moreno Jul 18 '14 at 15:28
  • @AngelS.Moreno the example in my question is a simplified version of a more complex plugin load. It should only load routes for the plugins that have `'routes' => true`, but this isn't working. Changing my code to use a global inclusion of routes with `'ignoreMissing' => true` resolves the issue (fixed thanks to the comments in Rakesh's answer below). It's not ideal though as it means it will load all plugin routes even if I don't want them for a particular plugin. Hope that makes things clearer. – drmonkeyninja Jul 18 '14 at 15:40
  • so you think your code should run like this: load all the plugins but only those that have routes? I am a bit confused with what your goal and expected results are. LoadAll loads all the plugins. If you want to handpick which plugins to load or which plugins to load routes for then don't use loadAll. Am I misunderstanding you? – Angel S. Moreno Jul 18 '14 at 19:25

1 Answers1

0

I gave a look at CakePlugin::loadAll() function and it looks like you are using function parameters in wrong way.

Here is a actual function:

public static function loadAll($options = array()) {
    $plugins = App::objects('plugins');
    foreach ($plugins as $p) {
        $opts = isset($options[$p]) ? (array)$options[$p] : array();
        if (isset($options[0])) {
            $opts += $options[0];
        }
        self::load($p, $opts);
    }
}

Which translate to - For each plugin:

  • Check if option has plugin name as key, if yes consider it's value as plugin settings
  • If option has numeric key (0), i.e. option without any key name, consider it as global plugin setting and add it to every plugin's setting

Just remove extra array in your options, and it should be fine.

CakePlugin::loadAll(array(
    'MyPlugin' => array(
        'routes' => true
    )
));
  • This is the way we originally had our code, but it didn't load any of the plugins in that form. According to the example code in Cake 2.5 it is double wrapping the plugins as I showed in my example code. It doesn't feel right, but without that extra array() it couldn't find the plugins. – drmonkeyninja Jul 17 '14 at 10:15
  • @drmonkeyninja Even in example, there is no double array. Just that first element is array without key. You can read it as: CakePlugin::loadAll(array( 0 => array('bootstrap' => true), 'MyPlugin' => array( 'routes' => true ) )); – Rakesh Tembhurne Jul 17 '14 at 10:36
  • If I pass an array without a key as the first item it does work, but this doesn't seem to be right, despite the fact that it resolves the issue. This fixes it: CakePlugin::loadAll(array( [], 'MyPlugin' => array( 'routes' => true ) )); – drmonkeyninja Jul 18 '14 at 08:53