Mario
Mario

Reputation: 21

CakePHP - Trying to use an image instead of a text to link to somewhere else

I´m quite new to CakePHP ... I am using CakePHP 3 and want display an image in a <td>, that links to an URL or a Javascript function.

$this->Html->tag('td', $this->Html->link($this->Html->image('/img/do_something.png'), array('action' => 'do_something')))

My problem is, that the image isn´t displayed but instead it shows:

<img src="/img/do_something.png" alt=""/>

Any ideas ?

Upvotes: 1

Views: 441

Answers (3)

RaJ
RaJ

Reputation: 1

Simply Do :

    $this->Html->image('image.jpg',['url' => ['action' => 'action_name']]
                      );

Upvotes: 0

drmonkeyninja
drmonkeyninja

Reputation: 8540

burzum's answer about using 'escape' => false on the link works great and gives you the ability to easily set attributes on both the link and image. However, if you just simply want to link an image you can do this by setting the url option on the image:-

$this->Html->image(
    '/do_something.png', 
    [
        'url' => ['action' => 'do_something']
    ]
);

Which will give you something like:-

<a href="/controller/do_something">
    <img src="/img/do_something.png" alt="" />
</a>

As burzum mentioned, it's all clearly explained in the docs.

Upvotes: 2

floriank
floriank

Reputation: 25698

Reading the manual usually helps:

HTML special characters in $title will be converted to HTML entities. To disable this conversion, set the escape option to false in the $options array.

echo $this->Html->link(
    $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]),
    "recipes/view/6",
    ['escape' => false]
);

Will output:

<a href="/recipes/view/6">
    <img src="/img/recipes/6.jpg" alt="Brownies" />
</a>

Upvotes: 2

Related Questions