CakePHP HTML image links

CakePHP HTML image links FAQ: How do I create HTML image links in CakePHP?

When I first started using CakePHP I had no idea how to create a CakePHP image link, so I thought I'd share what I've learned here, specifically the CakePHP HTML image and HTML link syntax.

Rather than go into any great detail, I'll just share the source code from one of my CakePHP view pages. This is from a typical CakePHP index view page, but I wanted to use images for the Edit and Delete actions instead of plain links. Here's the CakePHP view code I created to get this done:

<td class="actions">
  <?php
  # create references to the images
  $edit_img = $html->image('edit.gif', array('alt' => 'Edit', 'title'=>'Edit Project'));
  $delete_img = $html->image('trash.gif', array('alt' => 'Delete', 'title'=>'Delete Project'));

  # use the images in CakePHP HTML links
  echo $this->Html->link($edit_img, array('action' => 'edit', $project['Project']['id']), array('escape' => false)); 
  echo $this->Html->link($delete_img, array('action' => 'delete', $project['Project']['id']), array('escape' => false), sprintf(__('Are you sure you want to delete # %s?', true), $project['Project']['id']));
  ?>
</td>

I think the only thing you need to know besides this source code is that these images are stored in this CakePHP directory:

app/webroot/img

Of course it helps if your images are somewhere around 16-32 pixels tall, but that's not something I can help you with, lol.

I hope this CakePHP image links tip has been helpful.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.