【问题标题】:Cakephp Image not showing in view.ctpCakephp 图像未显示在 view.ctp 中
【发布时间】:2019-11-03 12:10:30
【问题描述】:

我想使用 CakePHP 在我的页面中插入一个评级星。图像的路径在 javascript 中,如下所示。但是,图像没有显示。

$.fn.jRating = function(op) {
        var defaults = {
            /** String vars **/
            bigStarsPath : '../img/icons/stars.png' , // path of the icon stars.png
            smallStarsPath : '../img/icons/small.png', // path of the icon small.png
            phpPath : 'app/jRating.php', // path of the php file jRating.php
            type : 'big', // can be set to 'small' or 'big'

我已经尝试使用<?php echo $this->webroot; ?>img/icons/stars.png。但没有运气。谁能帮帮我。

【问题讨论】:

  • 使用绝对网址 - 你为什么要引用“应用程序”?
  • 对不起,我不明白你的意思。你能解释一下'绝对网址'
  • bigStarsPath : '.. Here's a similar question with the same problem
  • 我已经尝试过 bigStarsPath : 'img/icons/stars.png',但图像仍然没有显示。
  • 这并不奇怪,因为这也是一个相对路径。查看您请求的图像(使用浏览器的开发工具或服务器访问日志)并将其与图像的实际位置进行比较。

标签: javascript php cakephp


【解决方案1】:

我在布局中总是做的是这样放置一个 javascript 变量:

<script type="text/javascript">
var root = '<?php echo $this->Html->url('/'); ?>';
</script>

这样您始终可以参考项目的根目录。

那么你的代码将是:

$.fn.jRating = function(op) {
    var defaults = {
        /** String vars **/
        bigStarsPath : root+'img/icons/stars.png' , // path of the icon stars.png
        smallStarsPath : root+'img/icons/small.png', // path of the icon small.png
        phpPath : 'app/jRating.php', // path of the php file jRating.php
        type : 'big', // can be set to 'small' or 'big'

我在这里看到的问题是 phpPath 指向 app/,我认为这是不可见的,因为项目的根目录设置为 app/webroot/。

因此,要么将您的文件 jRating.php 移动到 app/webroot/,要么将逻辑移动到控制器的操作中。

【讨论】:

  • 合理建议,+1。 phpPath 可能也需要这种治疗,即 + phpPath : root + 'jRating.php'
  • 谢谢尼克·尚科夫!这真的很有效。我要学习一些新东西;p
【解决方案2】:

请改用$this-&gt;Html-&gt;image()。该函数检索并添加到位于app/webroot/img 中的图像的路由。

文档:http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::image

【讨论】:

    【解决方案3】:

    使用 html 帮助器并将 URL 设置为如下所示:

    ../folder-name-in-webroot/file-name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多