我想创建一个小部件,这是我做的步骤:
widgets
中创建了文件夹protected
。views
中创建了文件夹widgets
。config/main.php
:'application.widgets.*'
widgets/Alert.php
:class AlertWidget extends 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]);
}
}
widgets/views/alert.php
:echo $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
答案 0 :(得分:2)
如果您要使用$this->widget('application.widgets.Alert');
访问窗口小部件,则窗口小部件类名称应为Alert
(例如:public class Alert extends CWidget...
),文件名应保持为Alert.php