【问题标题】:CakePHP Absolute Image URLsCakePHP 绝对图像 URL
【发布时间】:2016-05-05 18:39:05
【问题描述】:

我希望使用 CakePHP 中的 $html->image(...) 辅助函数来输出图像,但我需要它使用绝对 URL 而不是相对 URL 生成 img 标记(生成的 HTML 将被下载并通过电子邮件发送到公司通讯)。这可能吗?

它没有记录,但我从查看源代码中注意到image 函数可以将数组作为其第一个参数。不过,我并不完全清楚如何使这项工作正常工作 - 以这种方式尝试这样做会产生相对于当前页面而不是在 webroot/img 文件夹中的图像 URL。

【问题讨论】:

    标签: url cakephp


    【解决方案1】:

    在 CakePHP 1.x 中,方法是:

    $this->Html->image($this->Html->url('/path_to_image/image.png',true));
    

    在 CakePHP 2.x 中,方法是:

    $this->Html->image('/path_to_image/image.png', array('fullBase' => true));
    

    【讨论】:

    • @Jleagle 我更新了我的答案以在每个版本中指定正确的方法。 (当我最初发布这个答案时,1.3 是当前版本)
    【解决方案2】:

    正确的方法是在 CakePHP 2.x 中:

    <?php
    echo $this->Html->image("logo.png", array('fullBase' => true));
    

    直接从食谱中提取的代码: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

    【讨论】:

      【解决方案3】:

      $html-&gt;image('http://example.com/path_to_image/image.png'); 应该可以解决问题。

      【讨论】:

      • 这应该是 CakePHP 的座右铭 :)
      • 如果我将网站移至 example2.com 会怎样?
      【解决方案4】:

      你应该可以使用:

      $html->image('http://[domain]/[path]/[image]')
      

      【讨论】:

        【解决方案5】:

        带链接图片:-

        <?php echo $this->Html->link($this->Html->image('logo.png',array('alt'=>'Logo')),'/', array('fullBase'=>true)); ?>
        

        仅显示图片:-

        <?php echo $this->Html->image('logo.png',array('alt'=>'Logo')),'/', array('fullBase'=>true); ?>
        

        【讨论】:

          【解决方案6】:

          有时最好保持最简单的方式

          【讨论】:

          • 在 Cake 的情况下,显然不是。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-31
          • 2023-03-30
          • 2011-09-18
          • 2018-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多