p. ! First things first: Helpers. The definition[1]: 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 helper[2] (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: With that in mind we can write our helper: function link($image, $url) 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: In our view we can then use our helper like: 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 component[3] (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: So we can write our component: function startup(&$controller) function goto($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: and are then able to use it in the following way: 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. 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. 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. function sayHello() 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 If you want to learn more about extending the CakePHP framework I recommend to look at some code Happy baking [1] Documentation for helpersHelpers
// app/views/helpers/image.php
class ImageHelper extends Helper
{
var $helpers = array('Html');
{
return $this->Html->link($this->Html->image($image), $url, null, false, false);
}
}var $helpers = array('Image');link('/cake.logo.png', 'http://cakephp.org'); ?>Components
// app/controllers/components/redirect.php
class RedirectComponent extends Object
{
var $controller;
var $components = array('RequestHandler');
{
$this->controller =& $controller;
}
{
if ($this->RequestHandler->isAjax())
{
$this->controller->set('url', $url);
}
else
{
$this->controller->redirect($url);
}
}
}
var $components = array('Redirect');
$this->Redirect->goto('/login');
Plug-ins
app/plugins
/helloworld
/controllers
/models
/views
// app/plugins/helloworld/helloworld_app_controller.php
class HelloworldAppController extends AppController
{
}
// app/plugins/helloworld/helloworld_app_model.php
class HelloworldAppModel extends AppModel
{
}
// app/plugins/helloworld/models/helloworld.php
class Helloworld extends HelloworldAppModel
{
var $useTable = false;
{
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
Learn more
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 CakeForge[4] or RosSoft’s blog[5]. Last, but not least, check out the CakePHP manual[6].
[2] API documentation of the Html helper
[3] API documentation of the RequestHandler component
[4] CakePHP Homepage
[5] RosSoft’s blog
[6] CakePHP manual


One comment to “Extending the CakePHP framework”
July 10th, 2007 at 7:05 am
I want to use helper in component. then how can i use.