CakePHP - 尝试使用图像而不是文本链接到其他地方

时间:2015-08-27 12:47:43

标签: cakephp cakephp-3.0

我是CakePHP的新手......我正在使用CakePHP 3并希望在<td>中显示一个链接到URL或Javascript函数的图像。

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

我的问题是,图片没有显示,而是显示:

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

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

Reading the manual通常会有所帮助:

  

$ title中的HTML特殊字符将转换为HTML实体。   要禁用此转换,将转义选项设置为false   $ options数组

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

将输出:

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

答案 1 :(得分:2)

burzum关于在链接上使用'escape' => false的答案很有用,并且您可以轻松地在链接和图像上设置属性。但是,如果您只是想链接图片,可以通过设置url option on the image: -

来完成此操作
$this->Html->image(
    '/do_something.png', 
    [
        'url' => ['action' => 'do_something']
    ]
);

这会给你类似的东西: -

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

正如burzum所说,这都是clearly explained in the docs

答案 2 :(得分:0)

简单地做:

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