Search code examples
phpzend-frameworkzend-viewview-helpersviewhelper

Unfound image in View_Helper cause multiple db query in another View_Helper


Very nasty problem, I made a very long investigation to find out what was the origin of the bug. I made an original post for this, but I deleted it to create a new fresh post. So let's start by the start. Thank you in advance for reading this until the end.

I have a View Helper Pub.php. This one display randomly an ad. $this->pub() is called in the layout.phtml and in the phtml view files. The Helper also increments the number of impression before displaying it.

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();
}

My layout.phtml is also simple :

<html>
<head>
   <?= $this->pub(null, 'css') ?>
</head>
<body>
   <?= $this->pub(null, 'big_banner' ?>
</body>

Problem : On some actions, ads in the layout are selectionned and incremented twice ! As if the helper was instancied again.

After some search, the problem seems to come from another View Helper : LogoEvent. This helper displays a logo/image by returning proper HTML code.

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" />';

    }
}

}

The double-incrementation happens when the file doesn't exist on my hard disk. Really weird... I tried this :

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

echo $this->logoEvent( 'unexisting_image.jpg', '100');
// => problem.

But

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

Someone has better knowledge than me to find out what could be the problem or a way to find it... Thank you !


Solution

  • I'm almost certain that your problem is from .htaccess, where, be default in ZF, is set to send all non-existing files (the -s condition) to index.php, thus your application will fire up again (possibly into the ErrorController, for 404).

    Add this in .htaccess instead, see how it fits (it omits certain files to be routed to index.php):
    RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [NC,L]