3.10.1 Using Helpers

You use helpers in CakePHP by making a controller aware of them. Each controller has a $helpers property that lists the helpers to be made available in the view. To enable a helper in your view, add the name of the helper to the controller’s $helpers array.

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

You can also add helpers from within an action, so they will only be available to that action and not the other actions in the controller. This saves processing power for the other actions that do not use the helper as well as help keep the controller better organized.

<?php
class BakeriesController extends AppController {
    function bake {
        $this->helpers[] = 'Time';
    }
    function mix {
        // The Time helper is not loaded here and thus not available
    }
}
?>
  1. <?php
  2. class BakeriesController extends AppController {
  3. function bake {
  4. $this->helpers[] = 'Time';
  5. }
  6. function mix {
  7. // The Time helper is not loaded here and thus not available
  8. }
  9. }
  10. ?>