Making a global plugins directory for CakePHP

This is a quick trick I learned from master baker gwoo. If by chance you have multiple projects, and those projects share multiple plugins. You are in a bit of a pickle. You either need to copy the plugins into each app and then update both when you make changes, or use something like svn:externals or git submodules. These all work quite well, however what if you don’t want to include debugKit or ApiGenerator in your repository? Well, there is another approach and it is the one that the venerable gwoo showed me.

Bootstrappin’!

First create a directory to hold all your plugins. Mine is ~/Sites/cake_plugins. Next put all your common plugins in there. So in mine I have only the best plugins like DebugKit and ApiGenerator. So my directory structure looks something like

Show Plain Text
  1. ~/Sites/cake_plugins/debug_kit
  2. ~/Sites/cake_plugins/api_generator

And so on. Next in each app you want to use your plugins in add the following to app/config/bootstrap.php.

Show Plain Text
  1. $pluginPaths = array(
  2.     '/Users/markstory/Sites/cake_plugins/',
  3. );

And there you have it, obviously unless you also have a /Users/markstory/ you might want to change the path you use to one that actually exists. Cake should automatically find your plugins in their new home. I find this setup ideal for development, as I can keep all my repositories separate, and include my global plugins quite easily. Thanks gwoo :)

Comments

Good thing I have a directory named Users/markstory/ else I don’t think I could of followed this complex tutorial :) Thanks for the tips it came in handy saves me the copy and paste time.

JB on 3/2/09

I agree with JB.

This tutorial is impossible to understand. What is this markstory directory anyways?

Do you really think every mother names their kid markstory?

keymaster on 3/2/09

You can use that tip for all cake includes directories !

In my company, we have created our framework based on cake (with specific plugins, helpers, or layouts), in order to start new projects very quickly : whe have created a “common” directory (at the same level than “app” and “cake”), including a “views”, “controllers”, “config” and “plugins” directories. We put all our common sources in that directory, and use that lines in our bootstraps :
$modelPaths = array(COMMON . ‘models’ . DS);
$behaviorPaths = array(COMMON . ‘models’ . DS . ‘behaviors’ . DS);
$helperPaths = array(COMMON . ‘views’ . DS . ‘helpers’ . DS);
$viewPaths = array(COMMON . ‘views’ . DS);
$componentPaths = array(COMMON . ‘controllers’ . DS . ‘components’ . DS);
$pluginPaths = array(COMMON . ‘plugins’ . DS);
$vendorPaths = array(COMMON . ‘vendors’ . DS);
Configure::buildPaths(compact(‘modelPaths’, ‘viewPaths’, ‘controllerPaths’, ‘helperPaths’, ‘componentPaths’, ‘behaviorPaths’, ‘pluginPaths’, ‘vendorPaths’));

Done !

Sébastien Charrier on 3/2/09

Or you can make a symlink of that plugin dir in your app dir I guess. And if you do it on a production environment then it wouldn’t break if theire isn’t a /User/markstory/Sites/etc….

primeminister on 3/2/09

Sébastien: I don’t think you need the buildPaths() at the end of your bootstrap, I’m pretty sure those variables will be collected by Configure anyways. But using paths to add in a base set of classes is an excellent use of the $fooPaths vars in my opinion, thanks for sharing.

primeminister: Of course a symlink could work, unless you were on windows. I wouldn’t recommend using an absolute path like I did, I think using a constant like Sébastien showed is a better approach.

mark story on 3/2/09

Sébastien and Mark: Thank you both very much for this ever so useful method, and thank you Mark for your fantastic blog. By the way, Mark… LOVE the theme on your blog. Very classy bit of graphic design/web design. :)

Silver Knight on 3/2/09

I agree with primeminister, I also use symbolic links to achieve the same thing. It is especially useful to link user upload directories into TMP . ‘/uploads’.

Paul on 3/2/09

Great idea. Thanks for sharing. I have the whole cake/libs part in an dir, where apache does not has access and I have put into index.php the path of where the cake core stuff is. This way I have a global vendors dir where I could put all the different vendor stuff I use. Now I will combine this with the bootstrap idea.

Siegfried on 3/3/09

yes, of course this is a foolish idea. primeminister says the right thing! Dont let you mislead!

yes on 3/3/09

@mark story: windows?!!?? ;)

primeminister on 3/4/09

yes: I wasn’t trying to mislead anyone, just trying to show an alternative.

mark story on 3/5/09

I had written up an article in this same vein : http://bakery.cakephp.org/articles/view/maintaining-an-application-independant-code-library

Eduardo on 3/6/09

Looking at the date I wonder if it stills a good way to manage shared plugins. Is there anything better now?

DJ on 2/28/12

Comments are not open at this time.