如何在cakephp中添加link内的内容

How to add content inside link in cakephp

我想用 cakephp 在我的 link 中添加内容。我知道我可以使用此语法在我的 link

中添加图像
$this->Html->image("recipes/6.jpg", [
                   "alt" => "Brownies",
                   'url' => ['controller' => 'Recipes', 'action' => 'view', 6]
                   ]);

对于这样的 HTML 输出:

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

但是上面的代码并不是我想要的。我想要以下 HTML 输出

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

我用这段代码 可以工作,但我必须找出完整路径 link。

<a href="<?= '/recipes/view/6' ?>">
    <div>
        <?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
    </div>
</a>

有没有更强大的方法来用 cakephp 做我想做的事情?

我不确定这是否是一种更可靠的方法,但您可以这样做:

<?php
    echo $this->Html->link(
        $this->Html->div(null, $this->Html->image('/img/recipe/6.jpg')), 
        array('controller' => 'recipes','action' => 'view', 6), 
        array('escape' => false)
    );
?>

输出将是这样的:

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

您可以使用 LinkURL 机制。

Using Link


echo $this->Html->link( 
        $this->Html->div(null, 
                         $this->Html->image('recipes/6.jpg', ['alt' => 'Brownies'])), 
        array('controller' => 'Recipes', 'action' => 'view', 6), 
        array('escape' => false)
);

Using URL

在 HTML

中添加 URL
<a href="<?= $this->Html->url( array('controller' => 'Recipes', 'action' => 'view', 6), 
                               array('escape' => false)); ?>" class="light_blue">
  <div>
        <?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
  </div>

</a>