在View_Helper中找不到的图像导致在另一个View_Helper中进行多个db查询


Unfound image in View_Helper cause multiple db query in another View_Helper

非常棘手的问题,我做了很长时间的调查,以找出bug的来源。我为这个做了一个原始的帖子,但我删除了它来创建一个新的帖子。让我们从头开始。提前感谢你读完这篇文章。

我有一个视图助手Pub.php。这个程序随机显示一个广告。$this->pub()在布局中被调用。在PHTML视图文件中。Helper还会在显示之前增加印象数。

Pub.php

Class My_View_Helper_Pub extends Zend_View_Helper_Abstract {
public function pub( $place, $format ) {
    // Extract the active campaigns, then randomly select one
    $campaigns = $model->getActiveCampaigns();
    ...
    // Increase the number of view for this campaign (in MySQL)
    $model->increasePrint($campaign->id);
    // And display the banner
    echo $this->generateHtml($campaign->filename);
}
public function generateHtml($filename){
    // Generate the html according to the filename (image, flash, custom tag etc...)
    return $code;
}

IncreasePrint ()

public function increasePrint($id){
    $table = $this->getTable();
    $row = $table->find($id)->current();
    $row->imp_made = $row->imp_made + 1;
    return $row->save();
}
我的<<p> strong>布局。php 也很简单:
<html>
<head>
   <?= $this->pub(null, 'css') ?>
</head>
<body>
   <?= $this->pub(null, 'big_banner' ?>
</body>

问题:在某些操作中,布局中的广告被选择并增加两次!就像再次实例化了helper。

经过一番搜索,问题似乎来自另一个视图助手:LogoEvent。这个帮助器通过返回适当的HTML代码来显示徽标/图像。

LogoEvent.php

class My_View_Helper_LogoEvent extends Zend_View_Helper_Abstract
{
    public function logoEvent($image, $taille = null){
        $image = trim($image);
        if ($taille){
            $width = "max-width: {$taille}px;";
    } else {
        $width = '';
    }
    if (!empty($image)){
        return '<img src="/images/agenda/logos/'. $image .'" style="'. $width .'" alt="Logo" />';
    } else {
        return '<img src="/images/agenda/logos/no-logo.png" style="'. $width .'" alt="No Logo" />';
    }
}

}

当文件在我的硬盘上不存在时,双增量发生。很奇怪…我试过了:
echo $this->logoEvent( 'existing_image.jpg', '100');
// No problem, everything works fine.
echo $this->logoEvent( 'unexisting_image.jpg', '100');
// => problem.

,

 echo htmlentities($this->logoEvent( 'unexisting_image.jpg', '100'));
 // No problem, everything works fine.

有人比我更有知识,可以找出问题所在或找到问题的方法……

我几乎可以肯定,您的问题来自。htaccess,其中,默认在ZF中,设置为发送所有不存在的文件(-s条件)到index.php,因此您的应用程序将再次启动(可能进入ErrorController,为404)。

将其添加到.htaccess中,看看效果如何(它省略了要路由到index.php的某些文件):
RewriteRule !'.(js|ico|gif|jpg|png|css)$ index.php [NC,L]