0
votes

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?

1
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
@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
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
@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
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

1 Answers

0
votes

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