how to make widget in Yii

I want to create a widget, here is the steps I made:

  • created folder widgets in folder protected .
  • created folder views in folder widgets .
  • added this in config/main.php : 'application.widgets.*'
  • this is the code of 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]);
        }
    }
    
  • this is the code of widgets/views/alert.php :
  • echo $data;
    
  • this is the code to how I use the widget in a view:
  • $this->widget('application.widgets.Alert');
    

    finally I got these errors:

    ( ! ) SCREAM: Error suppression ignored for
    ( ! ) Fatal error: Cannot redeclare class AlertWidget in C:wampwwwmediastoreprotectedwidgetsAlert.php on line 27
    

    if you're going to access the widget using $this->widget('application.widgets.Alert'); then, the widget class name should be Alert (like: public class Alert extends CWidget... ) and the filename should remain Alert.php

    链接地址: http://www.djcxy.com/p/69730.html

    上一篇: JQuery添加/删除类不工作的第二次

    下一篇: 如何在Yii中制作小部件