我在Yii框架中遇到问题,我想在layout / main.php页面中调用控制器的动作,该页面属于siteController,我这样做了:
$a = UsersController::actionRequestAlert($s);
然后我收到了这个错误:
Non-static method UsersController::actionRequestAlert() should not be called statically, assuming $this from incompatible context
那我怎么解决这个问题呢?
确定, 现在我想创建一个小部件,这是我做的步骤:
'application.widgets.*'
这是小部件/ Alert.php的代码:
类AlertWidget扩展了CWidget { public $ alert = null;
private $_data = null;
public function init()
{
$s = Yii::app()->session['userId'];
$r = Requests::model()->findAll('idUser='.$s.' and confirm =0 and unconfirm=0 and cancel=0');
$i=0;
foreach($r as $x)
$i++;
if($i<=0)
$alert=null;
else
$alert="(".$i.")";
$this->_data = new CActiveDataProvider($alert);
}
public function run()
{
$this->render('alert', ['data' => $this->_data]);
}
}
这是小部件/ views / alert.php的代码:
echo $ data;
这是我在视图中使用窗口小部件的代码:
$这 - &GT;插件( 'application.widgets.Alert');
最后我遇到了这些错误:
( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: Cannot redeclare class AlertWidget in C:\wamp\www\mediastore\protected\widgets\Alert.php on line 27
答案 0 :(得分:1)
关于第一个问题: 您必须将方法actionRequestAlert()定义为静态
public static actionRequestAlert() {}