【问题标题】:How do I get the full URL to an image in Cakephp?如何获取 Cakephp 中图像的完整 URL?
【发布时间】:2013-04-06 00:53:39
【问题描述】:

假设我有一个“image.jpg”存储在资产文件夹中,路径是“www.example.com/public”,我只想要图像的字符串路径。我怎么得到这个?我不希望 html 标签只包含路径字符串。

【问题讨论】:

  • 你是说文件image.jpg存放在你给的路径吗?
  • 对不起,你什么意思?
  • 请详细说明您的问题。或者说清楚
  • string path to the image是什么意思
  • 如果资产文件夹中有图片,如何获取该图片的 URL?

标签: image url cakephp hyperlink path


【解决方案1】:

在尝试了各种 CakePHP 全局常量(ROOT、WEBROOT_DIR、WWW_ROOT、CSS 等)都没有结果后,似乎在返回应用程序 webroot 路径的 $this->webroot 属性中找到了通用解决方案。因此,对于上述各种情况,我们的布局、视图或元素中可能有:

一个简单的图片标签:

<img src="<?php echo $this->webroot; ?>img/foo.gif" .../ > 

表格单元格中的背景图像以旧方式:

<td background="<?php echo $this->webroot; ?>img/foo.gif"> 

type="image" 的输入:

<input type="image" src="<?php echo $this->webroot; ?>img/go_btn.gif" ... /> 
I think webroot always work

【讨论】:

【解决方案2】:

查看HtmlHelper::image() 的源代码,看看这个助手如何创建正确的 URL;稍作修改,是这样实现的:

$imageUrl = $this->assetUrl('image.jpg', array(
    // note: only required if you need the
    // URL -including- http://example.com
    'fullBase'   => true,
    'pathPrefix' => IMAGES_URL
));

Helper::assetUrl() 的源代码可以在这里找到:https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper.php#L305

【讨论】:

    【解决方案3】:

    在 CakePHP 3(从 3.2.4 版本开始),您可以使用 url/image 助手,但路径不是必需的:

    $this->Url->image('image.jpg');
    

    或其他资产:

    // Outputs /js/app.js
    $this->Url->script('my.js');
    
    // Outputs /css/app.css
    $this->Url->css('my.css');
    

    【讨论】:

    • 问题是要有完整的网址,而不是必要与否。这不起作用,例如,如果您需要电子邮件中的图像。这就是存在“fullbase”选项的原因。
    【解决方案4】:
    $imageUrl = $this->Html->assetUrl('image.jpg', array( // 注意:只有在需要时才需要 // URL - 包括 - http://example.com 'fullBase' => 真, 'pathPrefix' => IMAGES_URL ));

    即使您使用主题也可以使用

    【讨论】:

    • 工作,除了我必须使用:'pathPrefix' =&gt; Configure::read('App.imageBaseUrl') 因为 imageBaseUrl 是在 core.php 中定义的
    • 在 Cake 3.0 中工作。这是 Cakephp 最好和最可靠的答案。应该这样标记。
    【解决方案5】:
    $this->Html->url('/img/image.jpg') // returns /cake/app/img/image.jpg
    

    可以选择将true 传递给示例一,以获得完整的基本 URL。 (mysite.com/cake/app/img/image.jpg)

    WWW_ROOT . 'img/image.jpg' // full/path/to/webroot/img/image.jpg
    

    【讨论】:

    • 嘿,我正在尝试 Html->url 但我收到“调用非对象上的成员函数 url()”错误。我已经包含了“public $helpers = array('Html', 'Form');”在我的控制器中...
    • @JohnathanAu 你用的是什么版本的 CakePHP?
    • 这是一个 CakePHP 辅助函数,它只在视图中可用,在控制器中不可用。这是您收到该错误的最可能原因。
    • 您没有在控制器中指定。在别处使用WWW_ROOT . 'path/to/file.jpg'
    • 只是添加到@Ross 的答案中,WWW_ROOT 是服务器的本地路径。 $this-&gt;Html-&gt;url(...) 将生成一个特定于提供站点服务的域的 URL。哦,在构建本地路径时使用DS 常量而不是使用反斜杠通常是更好的做法,因为它依赖于平台。使用DS 意味着您将能够在任何环境中进行部署。
    【解决方案6】:

    你可以使用:

    $this->webroot.'img/abc.jpg'
    

    $this->webroot 将为您提供应用的当前路径。

    【讨论】:

      【解决方案7】:

      在 CakePHP 中,通常图片会存储在 webroot/img 文件夹中。

      这个link会对你有所帮助。

           HtmlHelper::image(string $path, array $options = array())
      

      参数:

           $path (string) – Path to the image.
      
           $options (array) – An array of html attributes.
      

      希望你觉得这很有用。

      【讨论】:

        【解决方案8】:

        在 CakePHP 版本 3 中,您可以使用 url 助手:

        $this->Url->build('/img/image.jpg');
        

        【讨论】:

          【解决方案9】:

          检查 cakephp 2.0+ FULL_BASE_URL 提供的这个常量

          click here了解所有

          【讨论】:

          • 也适用于 CakePHP 1.2.2!谢谢你的提示!
          • note CakePHP : Deprecated since version 2.4: This constant is deprecated, you should use Router::fullbaseUrl() instead.
          【解决方案10】:

          我会用

          $this->Html->url('/', true).'img/image.jpg';
          

          【讨论】:

            【解决方案11】:

            从 CakePHP 3.5 开始,我认为最好的选择是:

            $imgUrl = Cake\Routing\Router('/', true) . 'img/' . $imageName;
            

            如果您在插件范围内,$this-&gt;webroot 不适合。

            希望对某人有所帮助。

            【讨论】:

              【解决方案12】:

              示例:http://localhost:8080/ninavendas/rhhealth_vendas/pessoa/importar

              在 cake 3+ 中,您可以使用:$this->request->webroot

              返回:/ninavendas/rhhealth_vendas/

              【讨论】:

                【解决方案13】:

                fullBase 选项 - 返回带有域名的完整 URL

                $this->Url->image('image.jpg', ['fullBase'=>true]);
                

                【讨论】:

                  【解决方案14】:

                  CakePHP 3

                  使用 Cake\Routing\Router;

                  <img src="<?php echo Router::url('/', true); ?>/img/name.jpg" />
                  

                  【讨论】:

                    【解决方案15】:

                    您可以使用此代码...

                    <?php $image = $this->Html->image('../../site/webroot/img/folder_name/'.$content->image_path.''); ?>
                    

                    【讨论】:

                      【解决方案16】:

                      我认为您需要自己编写路径。如果您知道文件的名称和它所在的目录,那么是什么阻止了您执行以下操作:

                      $imageUrl = sprintf("%s/%s/%s", FULL_BASE_URL, "public", $imageName);
                      //  this produces http://www.example.com/public/image.png
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2018-08-28
                        • 2021-10-29
                        • 1970-01-01
                        • 1970-01-01
                        • 2021-04-05
                        • 1970-01-01
                        • 2019-04-08
                        • 2012-04-28
                        相关资源
                        最近更新 更多