【问题标题】:how to make widget in Yii如何在 Yii 中制作小部件
【发布时间】:2013-10-09 11:19:56
【问题描述】:

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

  • 在文件夹protected 中创建了文件夹widgets
  • 在文件夹widgets 中创建了文件夹views
  • 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

【问题讨论】:

  • 您应该将文件命名为与类名相同。你的意思是class Alert extends CWidget

标签: php yii widget


【解决方案1】:

如果您要使用$this-&gt;widget('application.widgets.Alert'); 访问小部件,那么小部件类名应为Alert(如:public class Alert extends CWidget...),文件名应保持为Alert.php

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 2021-04-17
    • 2016-01-26
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    相关资源
    最近更新 更多