Oct 24 2008
Using Images in CakePHP links
(This is the second post in a series of posts on CakePHP tips.)
The CakePHP link helper is a handy tool to create links in your application. Here is the basic syntax:
<?php echo $html->link('help!','/help'); ?>
And there’s a helper for creating images, too:
<?php echo $html->image('add.gif'); ?>
But what if you want to use an image in the link? If you try this:
<?php echo $html->link($html->image('add.gif'),'/customers/add')?>
You won’t get the image, but the HTML code of the image as the link. Lame!
CakePHP has built-in protection against SQL injection and cross site scripting attacks, so it doesn’t like outputting HTML without making it safe. So you need to turn off the HTML escaping to make the image work:
<?php echo $html->link($html->image('add.gif'),'/customers/add',array('escape'=>false))?>
Just pass in and set the escape parameter to false and you are home free.