Welcome to the Cookbook

loading...

3.11.2 Creating Helpers

If a core helper (or one showcased on Cakeforge or the Bakery) doesn’t fit your needs, helpers are easy to create.

Let's say we want to create a helper that could be used to output a specifically crafted CSS-styled link you need in many different places in your application. In order to fit your logic into CakePHP's existing helper structure, you'll need to create a new class in /app/views/helpers. Let's call our helper LinkHelper. The actual PHP class file would look something like this:

<?php
/* /app/views/helpers/link.php */

class LinkHelper extends AppHelper {
    function makeEdit($title, $url) {
        // Logic to create specially formatted link goes here...
    }
}

?>
  1. <?php
  2. /* /app/views/helpers/link.php */
  3. class LinkHelper extends AppHelper {
  4. function makeEdit($title, $url) {
  5. // Logic to create specially formatted link goes here...
  6. }
  7. }
  8. ?>

3.11.2.1 Including other Helpers

You may wish to use some functionality already existing in another helper. To do so, you can specify helpers you wish to use with a $helpers array, formatted just as you would in a controller.

<?php
/* /app/views/helpers/link.php (using other helpers) */
class LinkHelper extends AppHelper {
    var $helpers = array('Html');

    function makeEdit($title, $url) {
        // Use the HTML helper to output
        // formatted data:

        $link = $this->Html->link($title, $url, array('class' => 'edit'));

        return "<div class=\"editOuter\">$link</div>";
    }
}
?>
  1. <?php
  2. /* /app/views/helpers/link.php (using other helpers) */
  3. class LinkHelper extends AppHelper {
  4. var $helpers = array('Html');
  5. function makeEdit($title, $url) {
  6. // Use the HTML helper to output
  7. // formatted data:
  8. $link = $this->Html->link($title, $url, array('class' => 'edit'));
  9. return "<div class=\"editOuter\">$link</div>";
  10. }
  11. }
  12. ?>

3.11.2.2 Callback method

Helpers feature a callback used by the parent controller class.

beforeRender()

The beforeRender method is called after the controller's beforeRender method but before the controller's renders views and layout.

3.11.2.3 Using your Helper

Once you've created your helper and placed it in /app/views/helpers/, enable the helper in your view by adding the name of the helper to the controller’s $helpers array.

<?php
class BakeriesController extends AppController {
    var $helpers = array('Form', 'Html', 'Link');
}
?>
  1. <?php
  2. class BakeriesController extends AppController {
  3. var $helpers = array('Form', 'Html', 'Link');
  4. }
  5. ?>

Once your controller has been made aware of this new class, you can use it in your views by accessing an object named after the helper:

<!-- make a link using the new helper -->
<?php echo $this->Link->makeEdit('Change this Recipe', '/recipes/edit/5'); ?>
  1. <!-- make a link using the new helper -->
  2. <?php echo $this->Link->makeEdit('Change this Recipe', '/recipes/edit/5'); ?>

This is the new syntax introduced in 1.3. You can also access helpers using the form $link->makeEdit(), however the newer format allows view variables and helpers to share names and not create collisions.

The Html, Form and Session (If sessions are enabled) helpers are always available.