yii2 $app裡面如何自訂群組件

來源:互聯網
上載者:User
有個的第三方包,通過composer安裝後我想直接通過\Yii::$app->wechat來執行個體化這個類,所以我就在web.php中的components數組中配置了如下

    'wechat' => [                'class' => 'EasyWeChat\Foundation\Application',            ],

會報如下錯誤Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".
我看了下該類執行個體化需要傳入一個數組作為設定檔,所以我有將代碼改成了如下:

    'wechat' => [                'class' => 'EasyWeChat\Foundation\Application',                'config' => [                    'debug'  => true,                    'app_id' => 'your-app-id',                    'secret' => 'you-secret',                    'token'  => 'easywechat',                    'log' => [                        'level' => 'debug',                        'file'  => '/tmp/easywechat.log', // XXX: 絕對路徑!!!!                    ],                        //...                ],            ],

但還是一樣,Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".

所以我想問下,想這樣執行個體化需要參數的,這參數怎麼傳入?

回複內容:

有個的第三方包,通過composer安裝後我想直接通過\Yii::$app->wechat來執行個體化這個類,所以我就在web.php中的components數組中配置了如下

    'wechat' => [                'class' => 'EasyWeChat\Foundation\Application',            ],

會報如下錯誤Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".
我看了下該類執行個體化需要傳入一個數組作為設定檔,所以我有將代碼改成了如下:

    'wechat' => [                'class' => 'EasyWeChat\Foundation\Application',                'config' => [                    'debug'  => true,                    'app_id' => 'your-app-id',                    'secret' => 'you-secret',                    'token'  => 'easywechat',                    'log' => [                        'level' => 'debug',                        'file'  => '/tmp/easywechat.log', // XXX: 絕對路徑!!!!                    ],                        //...                ],            ],

但還是一樣,Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".

所以我想問下,想這樣執行個體化需要參數的,這參數怎麼傳入?

在 bootstrap.php 中

Yii::$container->set(EasyWeChat\Foundation\Application::class, [], [[  'debug'  => true,  'app_id' => 'your-app-id',  'secret' => 'you-secret',  'token'  => 'easywechat',  'log' => [      'level' => 'debug',      'file'  => '/tmp/easywechat.log', // XXX: 絕對路徑!!!!  ],  //...],]);

在設定檔中,不支援配置 construct的參數。你需要通過配置 container 來告訴Yii在執行個體化的時候,把配置資訊注入的contruct 中

給你個DEMO

 public function actionWechat() {                $options = [            'debug'     => false,            'app_id'    => 'wx3f3ea1dd10a123445',            'secret'    => '63005e31fd123123123123',            'token'     => '123123123123123',            'log' => [                'level' => 'debug',                'file'  => '/tmp/easywechat.log',            ],            'verify'            // ...        ];        $openid = '123123123123123';        $message = 'hello wechat';        $app = new \EasyWeChat\Foundation\Application($options);        $result = $app->staff->message($message)->to($openid)->send();                var_dump($result);        die;    }

你這樣寫Yii::$app->params['wechat'];試試看

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.