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 wanted to create a helper that could be used to output a specifically crafted CSS-styled link you needed many different places in your application. In order to fit your logic in to 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. ?>

There are a few methods included in CakePHP's Helper class you might want to take advantage of:

output(string $string)

Use this function to hand any data back to your view.

<?php
function makeEdit($title, $url) {
    // Use the helper's output function to hand formatted
    // data back to the view:
    return $this->output(
        "<div class=\"editOuter\">
         <a href=\"$url\" class=\"edit\">$title</a>
         </div>"
    );
}
?>
  1. <?php
  2. function makeEdit($title, $url) {
  3. // Use the helper's output function to hand formatted
  4. // data back to the view:
  5. return $this->output(
  6. "<div class=\"editOuter\">
  7. <a href=\"$url\" class=\"edit\">$title</a>
  8. </div>"
  9. );
  10. }
  11. ?>

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 $this->output("<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 $this->output("<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/, you'll be able to include it in your controllers using the special variable $helpers.

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

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

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