如何在cakephp中添加链接内的内容

时间:2015-03-22 02:54:19

标签: cakephp-3.0

我想在我与cakephp的链接中有内容。我知道我可以使用这种语法在我的链接中有一个图像

$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>

我带来了这个代码可以工作,但我必须找出完整的路径链接。

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

使用cakephp有没有更健壮的方式做我想要的事情?

2 个答案:

答案 0 :(得分:1)

我不确定这是否是一种更强大的方式,但你可能会做这样的事情:

<?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>

答案 1 :(得分:0)

您可以使用LinkURL机制。

使用链接


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)
);

使用 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>
相关问题