Yii2 $ How to customize components in the app

Source: Internet
Author: User
Tags openid
There is a third-party package. after composer is installed, I want to instantiate this class directly through Yii: $ app-& amp; gt; wechat, so I am on the web. the following {code...} is configured in the components array of php ...} the following error is reported: Missingrequiredparameter & quot; config & quot ;... there is a third-party package. after being installed through composer, I want to directly use \Yii::$app->wechatTo instantiate this class, so I am web.phpIn componentsThe array is configured as follows:

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

The following error is reported:Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".
After reading this class instantiation, you need to input an array as the configuration file, so I changed the code to the following:

'Cmd' => ['class' => 'easywechat \ Foundation \ application', 'config' => ['debug' => true, 'app _ id' => 'Your-app-id', 'secret' => 'You-secret', 'token' => 'easychat ', 'log' => ['level' => 'debug', 'file' => '/tmp/easywechat. log', // XXX: absolute path !!!! ], //...],],

But it is still the same,Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".

So I would like to ask how to input the parameter to be instantiated in this way?

Reply content:

There is a third-party package. after being installed through composer, I want to directly use\Yii::$app->wechatTo instantiate this class, so I amweb.phpIncomponentsThe array is configured as follows:

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

The following error is reported:Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".
After reading this class instantiation, you need to input an array as the configuration file, so I changed the code to the following:

'Cmd' => ['class' => 'easywechat \ Foundation \ application', 'config' => ['debug' => true, 'app _ id' => 'Your-app-id', 'secret' => 'You-secret', 'token' => 'easychat ', 'log' => ['level' => 'debug', 'file' => '/tmp/easywechat. log', // XXX: absolute path !!!! ], //...],],

But it is still the same,Missing required parameter "config" when instantiating "EasyWeChat\Foundation\Application".

So I would like to ask how to input the parameter to be instantiated in this way?

Inbootstrap.phpMedium

Yii: $ container-> set (EasyWeChat \ Foundation \ Application: class, [], [['debug' => true, 'app _ id' => 'Your-app-id', 'secret' => 'You-secret', 'token' => 'easychat ', 'log' => ['level' => 'debug', 'file' => '/tmp/easywechat. log', // XXX: absolute path !!!! ], //...],]);

Configuration is not supported in the configuration fileconstruct. You need to configurecontainerTo tell Yii to inject configuration information during instantiation.contructMedium

Here is a 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;    }

You writeYii::$app->params['wechat'];Try it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.