如何在CakePHP中为插件确定HtmlHelper图像引用



仍然是这个cakePHP框架的新手,并且在使用插件时很难掌握如何创建helper url。使用cakePHP 1.3.12,大多数链接引用都得到正确的插件名称,然而,当试图引用图像时,这是失败的。

例如,以以下文件夹结构为例:

/app/webroot/img/
/app/plugins/myPluginName/:
                         /controllers/checkout
                         /webroot/img/add_item.png
                          :

视图中添加了以下代码片段:

$this->Html->link( $this->Html->image("add_item.png"),
                   array('controller' => 'checkout', 'action' => 'index'),
                   array('escape' => false)
);

当这个渲染时,我得到:

<a href="/myPluginName/checkout"><img src="/img/add_item.png" /></a>

这会破坏图像,因为位置应该是/myPluginName/img/add_item.png。是否有一种方法可以让图像正确引用,而不必在完整路径中硬编码?

不,你需要把插件放在路径中,如:

$this->Html->link($this->Html->image('/myPluginName/img/add_item.png'),
array('controller' => 'checkout', 'action' => 'index'),
array('escape' => false));

CakePHP手册中提到:"在img, js或css路径之前注意/your_plugin/前缀是很重要的。这让奇迹发生了!"

http://book.cakephp.org/view/1117/Plugin-assets

最新更新