View_Helper 中未找到图像导致另一个 View_Helper 中出现多个数据库查询
非常令人讨厌的问题,我进行了很长时间的调查以找出该错误的根源。我为此发表了一篇原创文章,但我删除了它以创建一个新的帖子。那么让我们从头开始吧。预先感谢您阅读本文直到最后。
我有一个 View Helper Pub.php。这个随机显示一个广告。 $this->pub() 在layout.phtml 和phtml 视图文件中调用。帮助程序还会在显示展示次数之前增加展示次数。
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();
}
我的 layout.phtml 也很简单:
<html>
<head>
<?= $this->pub(null, 'css') ?>
</head>
<body>
<?= $this->pub(null, 'big_banner' ?>
</body>
问题:在某些操作中,布局中的广告被选择并递增两次!仿佛帮手又被瞬间化了。
经过一番搜索,问题似乎来自另一个View 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.
有人比我有更好的知识来找出问题所在或找到它的方法...... 谢谢 !
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我几乎可以肯定您的问题来自 .htaccess,其中 ZF 默认设置为将所有不存在的文件(
-s
条件)发送到 index.php,因此您的应用程序将再次启动(可能进入 ErrorController,出现 404)。将其添加到 .htaccess 中,看看它如何适合(它省略了要路由到 index.php 的某些文件):
RewriteRule !\.(js|ico|gif|jpg|png|css)$index.php [NC,L]
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]