Search code examples
phpcakephpcakephp-2.x

Can I load a Plugin at cakephp with an alias?


Let's say that I install a plugin in CakePHP 2.x with name GmponosCakePlugin. This plugin would be under Plugins folder with Folder name GmponosCakePlugin.

To load the Plugin I need to do the following CakePlugin::load('GmponosCakePlugin');

Then if I need to use a Helper from this plugin I would have to use the following syntax GmponosCakePlugin.MyHelper inside my AppController in order to load the helper.

Is there any way I could have this plugin alias as Gmponos so I could load it as Gmponos.MyHelper


Solution

  • If you can't rename the folder, then the answer isn't necessarily no, but since there is no explicit plugin name aliasing functionality in CakePHP, all options you have are more or less "hacks".

    One option would be for example to register an additional fake plugin with the desired name, and a custom path that points to the actual plugin folder, like

    CakePlugin::load('Gmponos', array (
        'path' => APP . 'Plugin' . DS . 'GmponosCakePlugin' . DS
    ));
    

    This should work (not sure about possible side effects though), but it's kinda ugly. And it should be noted that this is in most cases required additionally to registering the normal plugin name, as in the plugin, code will refer to the actual plugin name.

    If it's your own plugin, and it is using composer, then changing the install folder name should be pretty easy via the installer-name extra option, which is the solution I'd prefer.

    https://github.com/composer/installers#custom-install-names

    You would ofcourse have to change possible plugin name references in the plugin code itself too.