1

I have some images inside links that I want to essentially look like this:

<a href="/path/to/img.png"><img src="/path/to/img.png" /></a>

Clicking on the link should load the image it contains. I'm trying to use CakePHP's HTML helper to do this, as follows:

<?php
  echo $html->link(
    $html->image('img.png'),
    'img.png',
    array('escape' => false)
  );
?>

When I do this, however, I get the following code:

<a href="/pages/img.png"><img src="/path/to/img.png" /></a>

Without using absolute URLs, can I make the link's href attribute point to the image's correct location?

Thanks!

2 Answers 2

4

you can also do this in 1.2

echo $html->link(
    $html->image('img.png'),
    'img.png',
    array(),
    null, 
    false
  );

or in 1.3

echo $html->link(
    $html->image('img.png'),
    'img.png',
    array(),
    array( 'escape' => false ),     
  );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, but by leaving the link URL as img.png Cake uses the Pages controller to handle it, making the URL example.com/pages/img.png. This is why the IMAGES_URL constant is needed. My code ended up looking like this: echo $html->link( $html->image('img.png'), '/' . IMAGES_URL . '/img.png', array('escape' => false) );
3

This should do the trick:

echo $html->image('image.png', array('url' => '/' . IMAGES_URL . 'image.png'));

1 Comment

Perfect! I didn't know about the IMAGES_URL constant. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.