Rav*_*wat 22
在尝试了没有结果的各种CakePHP全局常量(ROOT,WEBROOT_DIR,WWW_ROOT,CSS等)后,通常的解决方案似乎可以在$ this-> webroot属性中找到,该属性返回应用程序webroot的路径.因此,对于上面的各种情况,我们可能在我们的布局,视图或元素中:
一个简单的图片标签:
<img src="<?php echo $this->webroot; ?>img/foo.gif" .../ >
Run Code Online (Sandbox Code Playgroud)
表格单元格中的背景图像旧方法:
<td background="<?php echo $this->webroot; ?>img/foo.gif">
Run Code Online (Sandbox Code Playgroud)
输入type ="image":
<input type="image" src="<?php echo $this->webroot; ?>img/go_btn.gif" ... />
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)I think webroot always work
查看源代码,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
));
Run Code Online (Sandbox Code Playgroud)
Helper :: assetUrl()的源代码可以在这里找到:https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper.php#L305
小智 6
在CakePHP 3(版本3.2.4)中,您可以使用url/image帮助程序,但路径不是必需的:
$this->Url->image('image.jpg');
Run Code Online (Sandbox Code Playgroud)
或其他资产:
// Outputs /js/app.js
$this->Url->script('my.js');
// Outputs /css/app.css
$this->Url->css('my.css');
Run Code Online (Sandbox Code Playgroud)
小智 5
$imageUrl = $this->Html->assetUrl('image.jpg', array(
// note: only required if you need the
// URL -including- http://example.com
'fullBase' => true,
'pathPrefix' => IMAGES_URL
));
即使您使用主题,这也有效