13

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

4

16 回答 16

23

在尝试了各种 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
于 2013-04-16T07:02:25.660 回答
9

查看源代码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

于 2013-04-13T21:16:52.613 回答
7

在 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');
于 2016-07-01T09:34:03.240 回答
5
    $imageUrl = $this->Html->assetUrl('image.jpg', array(
        // 注意:只有在需要时才需要
        // URL - 包括 - http://example.com
        'fullBase' => 真,
        'pathPrefix' => IMAGES_URL
    ));

即使您使用主题,这也有效

于 2014-05-07T16:36:41.587 回答
2
$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
于 2013-04-13T15:54:05.840 回答
2

您可以使用:

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

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

于 2013-04-15T05:06:46.507 回答
1

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

链接将对您有所帮助。

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

参数:

     $path (string) – Path to the image.

     $options (array) – An array of html attributes.

希望你觉得这很有用。

于 2013-09-13T10:53:29.393 回答
1

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

$this->Url->build('/img/image.jpg');
于 2016-05-12T08:33:52.587 回答
0

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

点击这里了解所有

于 2013-04-13T18:24:54.873 回答
0

我会用

$this->Html->url('/', true).'img/image.jpg';
于 2014-03-25T18:14:08.013 回答
0

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

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

如果您在插件范围内,$this->webroot则不适合。

希望它可以帮助某人。

于 2018-04-11T08:43:23.200 回答
0

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

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

它的回报:/ninavendas/rhhealth_vendas/

于 2019-01-22T12:30:57.437 回答
0

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

$this->Url->image('image.jpg', ['fullBase'=>true]);
于 2019-10-03T12:39:12.723 回答
0

蛋糕PHP 3

使用蛋糕\路由\路由器;

<img src="<?php echo Router::url('/', true); ?>/img/name.jpg" />
于 2019-10-08T13:34:24.593 回答
0

您可以使用此代码...

<?php $image = $this->Html->image('../../site/webroot/img/folder_name/'.$content->image_path.''); ?>
于 2020-04-27T17:36:53.500 回答
-3

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

$imageUrl = sprintf("%s/%s/%s", FULL_BASE_URL, "public", $imageName);
//  this produces http://www.example.com/public/image.png
于 2013-04-13T17:46:53.847 回答