duanri1985 2016-10-17 12:05
浏览 18

无法将params传递给Yii小部件

I'm trying to refactor my code, and currently have a function in my component->controller to send slack notifications. However I want to extend notifications, so rather than have multiple functions, passing through params, i thought i'd put all notifications into a widget to be called within the controller.

I've compiled everything i need so far into a widget, but whenever i try and run this, i get the error

Undefined variable: type

Here's my widget being called in my controller when as item is actioned

$this->widget('application.components.DMNotifications', array(
                    'type' => 'slack',
                    'slack_content' => 'event_close',
                ));

And here's my component widget

<?php

class DMNotifications extends CWidget {

public $type = '';
    public $sendPermission = 0;
    //slack vars
    public $slack_content = null;
    public $newCompany = null;
    public $company = null;
    public $fullname = null;
    public $newInstance = null;

    public static $SLACK_REGISTRATION = "registration";
    public static $SLACK_EVENTPUBLISH = "event_publish";
    public static $SLACK_EVENTCLOSE = "event_close";
    public static $SLACK_EVENTCAPACITYTRIP = "event_capacity_trip";
    public static $SLACK_EMAILNOTIFICATIONS = "email_notifications";

    public function init() {
        if ($type === 1){
            $this->slack();
        }
        parent::init();
}

public function run() {
}

    public function slack(){

$sendPermission = ( Options::model()->isSlackWebhookOptDelegateReg() ? 1 : 0 );


$text = "You have a new message on the " . Options::model()->getSiteName();



        $url = Options::getSlackWebhookUrl();
        $json_data = json_encode(array(
                "text" => $text,
                "username" => "XX",
                "icon_url" => "xx.png",
                "channel" => Options::getSlackWebhookChannel()
        ));
        $post_data['payload'] = $json_data;
        if ($sendPermission === 1){
                $output = Yii::app()->curl->post($url, $post_data);
        }
    }

}

if i comment out the type error, i get a different error because nothing is being passed by the params. Either i've totally missed a blinding mistake, or what i'm trying to do isn't a widget.

Can anyone help???

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?