0

我在 Yii 框架中遇到问题,我想在属于 siteController 的 layout/main.php 页面中调用控制器的操作,我这样做了:

$a = UsersController::actionRequestAlert($s);

然后我收到了这个错误:

Non-static method UsersController::actionRequestAlert() should not be called statically, assuming $this from incompatible context

那么我该如何解决这个问题呢?


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

  • 在文件夹“受保护”中创建文件夹“小部件”。
  • 在文件夹“小部件”中创建文件夹“视图”。
  • 在 config/main.php 中添加了这个:'application.widgets.*'
  • 这是 widgets/Alert.php 的代码:

    类 AlertWidget 扩展 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 的代码:

    回显$数据;

  • 这是我如何在视图中使用小部件的代码:

    $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
4

1 回答 1

1

关于第一个问题:您必须将方法 actionRequestAlert() 定义为静态

public static actionRequestAlert() {}
于 2013-10-22T09:25:52.100 回答