3.10.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...
}
}
?>
<?php/* /app/views/helpers/link.php */class LinkHelper extends AppHelper {function makeEdit($title, $url) {// Logic to create specially formatted link goes here...}}?>
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>"
);
}
?>
<?phpfunction 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>");}?>
3.10.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 Helper {
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>");
}
}
?>
<?php/* /app/views/helpers/link.php (using other helpers) */class LinkHelper extends Helper {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>");}}?>
3.10.2.2 Using your Custom 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') ?>
<!-- make a link using the new helper --><?php echo $link->makeEdit('Change this Recipe', '/recipes/edit/5') ?>
Remember to include the FormHelper in the $helpers array if appropriate. The Html and Session (If sessions are enabled) helpers are always available.
