Joomla JError::raiseWarning()显示两次
我正在编写一个Joomla插件,它看起来或多或少像:Joomla JError::raiseWarning()显示两次,joomla,warnings,Joomla,Warnings,我正在编写一个Joomla插件,它看起来或多或少像: <?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); class plgSystemTest extends JPlugin { function onAfterInitialise(){ static $showWarnings = true;
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class plgSystemTest extends JPlugin {
function onAfterInitialise(){
static $showWarnings = true;
if($somecondition && $showWarnings)
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
?>
我正在Joomla1.5、1.6和1.7上测试这个插件。虽然警告确实会显示在每个页面中,但它会在页面第二次加载时显示两次
顺便说一下,还值得注意的是,$showWarnings
标志根本没有帮助
笔记
class plgSystemTest extends JPlugin {
function onAfterInitialise(){
static $showWarnings;
if ( !isset( $showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings;
function onAfterInitialise(){
if ( !isset( self::$showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings = true;
function onAfterInitialise(){
static $call_count = null;
if ( self::$showWarnings === true ) {
if( !isset( $call_count ) ) {
$call_count = 1;
} else {
$call_count++;
}
JError::raiseWarning(100,'Some warning that shows up twice. Total: ' . $call_count);
self::$showWarnings = false;
}
}
}
如果仍然不起作用,请尝试以下方法:
class plgSystemTest extends JPlugin {
function onAfterInitialise(){
static $showWarnings;
if ( !isset( $showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings;
function onAfterInitialise(){
if ( !isset( self::$showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings = true;
function onAfterInitialise(){
static $call_count = null;
if ( self::$showWarnings === true ) {
if( !isset( $call_count ) ) {
$call_count = 1;
} else {
$call_count++;
}
JError::raiseWarning(100,'Some warning that shows up twice. Total: ' . $call_count);
self::$showWarnings = false;
}
}
}
我希望有帮助
我正在编辑答案以回答您的评论
首先:对不起,我的代码出错了。在第二段代码中,您应该更改以下内容:
$showWarnings = false
为此:
self::$showWarnings = false;
如果仍不起作用,请尝试以下操作:
class plgSystemTest extends JPlugin {
function onAfterInitialise(){
static $showWarnings;
if ( !isset( $showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings;
function onAfterInitialise(){
if ( !isset( self::$showWarnings ) ) {
JError::raiseWarning(100,'Some warning that shows up twice.');
$showWarnings = false;
}
}
}
}
class plgSystemTest extends JPlugin {
protected static $showWarnings = true;
function onAfterInitialise(){
static $call_count = null;
if ( self::$showWarnings === true ) {
if( !isset( $call_count ) ) {
$call_count = 1;
} else {
$call_count++;
}
JError::raiseWarning(100,'Some warning that shows up twice. Total: ' . $call_count);
self::$showWarnings = false;
}
}
}
正如@alghimo所暗示的,警告以某种方式存储在会话中 不幸的是,我没有时间在Joomla调查这些奇怪的问题,所以技术细节仍然不清楚 我让它正常工作的方法是在
$\u GET['layout']
为编辑时不显示消息:
if($_REQUEST['layout'] != 'edit'){
JError::raiseWarning(100, 'Bla bla bla.');
}
也就是说,我觉得这不是这个问题的正确答案。如果有人想出更好的主意,我将非常乐意相应地交换答案。事实上,我一直在使用第二种变体,但根本没有用(Joomla的问题在于,我确信我编写的简单开关确实能按预期工作,但我认为对raiseWarning()
的调用以某种方式缓存在页面之间。是的,如果我没有错,排队消息存储在会话中,但如果您正在执行某种重定向(例如,“保存”),则只应收到两次此消息任务通常执行重定向)。在这种情况下,插件会添加两次消息。但我认为“新”任务不应该执行任何重定向