Making Clickable Images on CakePHP

Hello again,

Yesterday, i stumbled upon this problem on how am i suppose to create an image with a link on it. I needed to know it because of my goal. I wanted to use images as my buttons to navigate the admin pages. So without further ado, here’s how:

1. We will use HTML Helpers Image and Link, and we shall combine this 2.

2. For Image,

Syntax:

$html->image(string $path, array $htmlAttributes, boolean $return = false);

Example:

$html->image(‘/img/images/cancel.png’, array(‘class’ => ’save_button’));

3. For Link,

Syntax:

$html->link(string $title, string $url, array $htmlAttributes, string $confirmMessage = false, boolean $escapeTitle = true, boolean $return = false);

Example:

$html->link(‘SAVE’, ‘/registers’, array(), false, false, false);

4. So to make an image clickable,

<?php echo $html->link($html->image(‘/img/images/cancel.png’,array(‘class’ => ’save_button’)), ‘/registers’, array(), false, false, false); ?>

That’s it. I hope this will help you in the future.

4 Responses to “Making Clickable Images on CakePHP”

  1. Luke Says:
    Hi I would say you would be better for the URLs in more cake way, i.e.

    echo $html->link($html->image(’images/cancel.png’,array(’class’ => ’save_button’)), array(‘controller’=>’registers’,'action’=>’index’, array(), false, false, false);

    although it would work your way too.

    Just cleaner code in my opinion and for images it uses the default image folder syntax.
  2. Kalle Says:
    even more cleaner code

    you can add url in the options array for $html->image.

    $html->image(’images/cancel.png’,array(’class’ => ’save_button’, ´url´ => array(’controller’=>’registers’,’action’=>’index´))
  3. Mihai Says:
    Or you can try this:

    image(“images/cancel.png”, array(
    “alt” => “Cancel”,
    ‘url’ => array(‘controller’ => ‘registers’, ‘action’ => ‘index’)
    )); ?>
  4. Creating image links in CakePHP « Myles Kadusale’s Blog Says:
    [...] this tutorial from Here Check this page also Possibly related posts: (automatically generated)always use html->linkReal-Life Great [...]

Add Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>