duanliang789262 2013-10-09 09:18
浏览 37
已采纳

调用控制器动作在另一个控制器,制作小部件

I have a problem in Yii framework, I want to call a controller's action in the layout/main.php page which is belong to the siteController, I did this:

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

then I got this error:

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

so how can I solve this problem?


ok, now 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:\wamp\www\mediastore\protected\widgets\Alert.php on line 27
  • 写回答

1条回答 默认 最新

  • duanliushua5026 2013-10-22 09:25
    关注

    About first question: You must define method actionRequestAlert() as static

    public static actionRequestAlert() {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致