我在 Yii 框架中遇到了问题,我想在属于 siteController 的布局/主.php页面中调用控制器的操作,我这样做了:
$a = UsersController::actionRequestAlert($s);
然后我得到这个错误:
Non-static method UsersController::actionRequestAlert() should not be called statically, assuming $this from incompatible context
那么我该如何解决这个问题呢?
还行现在我想创建一个小部件,这是我所做的步骤:
- 在文件夹">
- 受保护"中创建文件夹"小部件"。 在文件夹">
- 小部件"中创建文件夹"视图"。
- 在配置/主中添加了这个.php:
'application.widgets.*'
这是小部件/警报的代码.php :
类 AlertWidget 扩展了 CWidget{ 公共$alert = 空;
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]); }
}
这是小部件/视图/警报的代码.php:
回声$data;
这是我如何在视图中使用小部件的代码:
$this->widget('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
关于第一个问题:您必须将方法操作请求警报((定义为静态
public static actionRequestAlert() {}