Categories


Loading feed
Loading feed
Loading feed

Extending the CakePHP framework


In this article I will show you how to extend the CakePHP framework with helpers, components, and plug-ins.

Helpers

First things first: Helpers. The definition1:

Helpers are meant to provide functions that are commonly needed in views to format and present data in useful ways.

Ok, let us write a simple helper, a helper for creating linked images. The functionality for creating a link and loading an image are already there, in the Html helper2 (one of the helpers shipped with CakePHP). So we use this helper within our own helper. There are also some conventions for helpers we have to follow:

  • The name of a helper class must end with “Helper”
  • A helper must be placed in app/views/helpers
  • A helper must extend the class “Helper”

With that in mind we can write our helper:

// app/views/helpers/image.php class ImageHelper extends Helper { var $helpers = array('Html'); function link($image, $url) { return $this->Html->link($this->Html->image($image), $url, null, false, false); } }

To use our helper, we have to add it to the helpers array in the controller which controls the view in which we want to use the helper:

var $helpers = array('Image');

In our view we can then use our helper like:

<?php echo $image->link('/cake.logo.png', 'http://cakephp.org'); ?>

Components

What helpers are for views are components for controllers. As you will see, components are very similar to helpers.

To get an impression of what components are, we will write a simple component for doing redirects. On the one hand the component should simply redirect when a “normal” request was made. And on the other hand a variable should be set for doing a client-side redirect when it was an Ajax request. The functionality to distinguish between “normal” request and Ajax request is already there, in the RequestHandler component3 (one of the components shipped with CakePHP). So we can use this component within our component. As with helpers there are some conventions we have to follow:

  • The name of a component class must end with “Component”
  • A component must be placed in app/controllers/components
  • A component must extend the class “Object” (I have no idea why this class is called “Object” and not “Component”)

So we can write our component:

// app/controllers/components/redirect.php class RedirectComponent extends Object { var $controller; var $components = array('RequestHandler'); function startup(&$controller) { $this->controller =& $controller; } function goto($url) { if ($this->RequestHandler->isAjax()) { $this->controller->set('url', $url); } else { $this->controller->redirect($url); } } }
Notice: The function “startup” is automatically called by the framework when the component is loaded.

The usage of our component is simple. We have to include it in the components array of our controller with:

var $components = array('Redirect');

and are then able to use it in the following way:

$this->Redirect->goto('/login');

Plug-ins

Rather new in the CakePHP world are plug-ins. Plug-ins are mini-applications, that means they can consist of models, views, and controllers. As before, we will write some code, a simple “hello world” plug-in.

First we have to create the necessary file structure for our plug-in. Each plug-in has its own folder within the app/plugins directory.

app/plugins
        /helloworld
            /controllers
            /models
            /views

The folder name is also the plug-in name. We need this name for the next step, the creation of AppController and AppModel for our plug-in, which are called HelloworldAppController, respectively HelloworldAppModel. These are the base classes for the controllers and models in our plug-in. In our case the classes are empty.

// app/plugins/helloworld/helloworld_app_controller.php class HelloworldAppController extends AppController { } // app/plugins/helloworld/helloworld_app_model.php class HelloworldAppModel extends AppModel { }

Now we can write our model, view and controller. This is almost identical to writing a “normal” application, the only difference is that our model and our controller have to extend the aforementioned classes.

// app/plugins/helloworld/models/helloworld.php class Helloworld extends HelloworldAppModel { var $useTable = false; function sayHello() { return 'hello world'; } } // app/plugins/helloworld/controllers/helloworld_controller.php class HelloworldController extends HelloworldAppController { function index() { $this->set('hello', $this->Helloworld->sayHello()); } } // app/plugins/helloworld/views/helloworld/index.thtml <?php echo $hello; ?>

As our controller has the same name as the plug-in, we can call it with myapp.com/helloworld . Otherwise we would have to use something like myapp.com/helloworld/mycontroller

Learn more

If you want to learn more about extending the CakePHP framework I recommend to look at some code ;) You find examples of helpers in /cake/libs/view/helpers, and examples of components in /cake/libs/controller/components. Other good ressources for examples are CakeForge4 or RosSoft’s blog5. Last, but not least, check out the CakePHP manual6.

Happy baking :)

[1] Documentation for helpers
[2] API documentation of the Html helper
[3] API documentation of the RequestHandler component
[4] CakePHP Homepage
[5] RosSoft’s blog
[6] CakePHP manual

Comments


Tuesday, July 10, 2007
HOW TO USE HELPER IN COMPONENT
12:05AM PDT · chintanfadia