CakePHP从表字段创建外部链接

时间:2015-01-26 20:44:21

标签: php cakephp

我是CakePHP的新手,但我很难从一个表中的一个字段中拉出一个URL来创建一个外部链接。它目前正在使用我们的域名,然后将网址放在其后。

有没有人知道如何强迫它使用外部婚礼地址?

以下是我使用的代码:

<a href="<?php echo $this->Html->url(array($result_array['Result']['results_video'])); ?>"><?php echo $this->Html->image('certificate_image.png', array('escape' => false)); ?></a>

2 个答案:

答案 0 :(得分:2)

如果网址已经是字符串:

<a href="<?php echo $result_array['Result']['results_video']; ?>">
    <?php echo $this->Html->image('certificate_image.png'); ?>
</a>

如果它是一个数组,你的代码是正确的,除了escape选项必须作为2nd arg进入link()方法而不是image()方法。

<a href="<?php $this->Html->url($result_array['Result']['results_video'], ['escape' => false]); ?>">
    <?php echo $this->Html->image('certificate_image.png'); ?>
</a>

如果您是CakePHP的新手,我建议您更频繁地查看API文档,了解方法所需的参数。 http://api.cakephp.org/2.6/class-HtmlHelper.html。大多数情况下,有一个选项数组可以做你想要的。

答案 1 :(得分:0)

HtmlHelper :: url和Router :: url采用url组件数组,例如:

$this->Html->url(array('controller' => 'page', 'action' => 'index'));
$this->Html->url($result_array['Result']['results_video']);

或字符串,例如:

$this->Html->url('http://www.google.com');
$this->Html->url($variable['Model']['url']);

你可以传递给url方法的第二个参数的唯一的东西是null或true,它将完整的基本url添加到从数组组件生成的url(如果为true),所以这不会是你传递给它的地方逃避价值,或者。

如果不确切知道$ result_array ['Result'] ['results_video']值中的内容,很难确切地说出你做错了什么,但我猜你真正想要的是这个,因为从上下文来看看起来你已经有了一个数组:

<?php
     echo $this->Html->link(
         $this->Html->image('certificate_image.png'),
         $result_array['Result']['results_video'],
         array('escape' => false)
     );
?>

(请注意,当您可以使用HtmlHelper :: link时,几乎不需要将值回显到<a>标记中。