如何在Yii中制作小部件

时间:2013-10-09 11:19:57

标签: php yii widget

我想创建一个小部件,这是我做的步骤:

  • 在文件夹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

1 个答案:

答案 0 :(得分:2)

如果您要使用$this->widget('application.widgets.Alert');访问窗口小部件,则窗口小部件类名称应为Alert(例如:public class Alert extends CWidget...),文件名应保持为Alert.php

相关问题