CakePower – a CakePHP Plugin

CakePHP is a great MVC framework I use every day for server side component of my applications and with CakeEvent introduction (v2.1) it become a really “app growing proof” framework.

Now it support logic injection architecture between plugins: you can expose some basic API or functionality into a plugin then you can extend or customize it from another plugin using CakeEvent and ViewBlocks.

CakePower is a collection of classes that add functionality to the framework or extend it with some real-app-oriented behaviors. This document will mantain an index of articles I write during this plugin developing.

Source Code Available on GitHub!

CakePower is an OpenSource and free project! You can follow us on Twitter and fork us on GitHub:

https://github.com/CakePower/CakePower

Static Classes

  • PowerSet – array utilities
  • PowerConfig - global access configuration with dotted notation path and convenient methods to investigate configuration params
  • PowerMenu – global object to define and alter menus
  • PowerApp – utilities to introspect your application (es lists all admin actions for authorization stuff)
  • PowerString – extends CakePHP string utilities

App Classes

Components

Helpers

  • PowerHtml – HtmlHelper extension
  • PowerTree – utility to output lists from a threaded data structure
  • PowerMenu – (PowerTree extension) utility to output lists form a PowerMenu path
  • PowerSession

Behaviors

  • IsActiveBehavior – allow to activate() and deactivate() models and filters them with ease

Other

  • LessCss framework integration - write better and optimized CSS!
  • pval() – check for params from post, get, session, config by name
  • REST – handling REST request the transparent way
  • PowerEventListener – a class to extend when writing Event Listener objects to interact with Cake Event System

How to install CakePower

CakePower is just a CakePHP plugin so you can put the “CakePower” folder into your app/plugins folder.

Then you need to edit your app/Config/bootstrap.php to teach Cake to load CakePower with it’s bootstrap:

/**
 * CakePower
 * You only need to load CakePower plugin with bootstrap.
 * Other plugins are loaded automagically by CakePower.
 */
CakePlugin::load( 'CakePower', array('bootstrap'=>true) );

NOTICE: you can remove other CakePlugin::load() instructions. CakePower will load all plugins found in the app. Learn how it’s easy to configure plugins automagically loading flow!

NOTICE: If you plan to put CakePower plugin into root/plugins folder you need to add a build path to your CakePHP bootstrap:

// root/plugins source path:
App::build(array(
    'Plugin' => array( ROOT . DS . 'plugins' . DS )
));

CakePower Bootstrap

CakePower starts it’s bootstrap loading some classes (like PowerConfig or UTH) then search for other plugins to be loaded.

Plugins initialization is really like CakePlugin::loadAll() but it look for both plugin’s “bootstrap” and “routes” files to automagically set up the configuration array to pass to CakePlugin class.

If a plugin contains a bootstrap.php CakePower will load it for you!
This is a very smart behavior when you have an application who needs to add/remove plugins in an automatic way.

plugin.php – configure plugin loading from inside!

Each plugin can implement an PluginPath/Config/plugin.php file who contains an array of options used by the CakePower bootstrap engine to configure the plugin loading method.

/App/Plugin/Foo/Config/plugin.php
$config = array(
    'order' => 10000,
    'load' => array( 'bootstrap'=>false, 'routes'=>true ),
    'var1' => 'value',
    'var2' => array( ... ),
    ...
);

CakePower sets the order key to a default value of “5000″. Plugins are sorted on this value before they are loaded. This means that plugin’s bootstrap.php and routes.php are imported following this order value.

CakePower compile the load key for you basing on bootstrap.php and routes.php existance in you plugin /Config folder. You can override this automation by setting here an arbitrary value according to CakePlugin::load() method.

CakePower will store the config key values into PowerConfig plugin’s key.
After the CakePower bootstrap we can access these configuration values this way:

CakePower::read('plugin.Foo.config.var1');

Using CakePower:

The better way to take advantage of CakePower is to extend it’s classes in your Controller/AppController.php script:

// Replace CakePHP standard declaration:
class AppController extends Controller {

// with this
class AppController extends CakePowerController {

A lot of magic happens this way!

You also need to extend CakePower in AppModel and AppHelper files too:

// app/Model/AppModel.php
class AppModel extends CakePowerModel {

// app/View/Helper/AppHelper.php
class AppHelper extends CakePowerHelper {

We are now working on provide a CakePowerComponent and a CakePowerBehavior to complete the layer between your App and CakePHP!