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() {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等