Home  >  Article  >  Backend Development  >  How to customize components in yii2 $app

How to customize components in yii2 $app

WBOY
WBOYOriginal
2016-07-06 13:51:471641browse

There is a third-party package for WeChat. After installing it through composer, I want to instantiate the WeChat class directly through Yii::$app->wechat, so I configured the following web.php in the components array in

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

The following error will be reportedMissing required parameter "config" when instantiating "EasyWeChatFoundationApplication".
I saw that the instantiation of this class requires an array to be passed in as a configuration file, so I changed the code to the following:

<code>
    '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: 绝对路径!!!!
                    ],
    
                    //...
                ],
            ],
</code>

But still the same, Missing required parameter "config" when instantiating "EasyWeChatFoundationApplication".

So I want to ask, if I want to instantiate something like this that requires parameters, how do I pass in the parameters?

Reply content:

There is a third-party package for WeChat. After installing it through composer, I want to instantiate the WeChat class directly through Yii::$app->wechat, so I configured the following web.php in the components array in

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

The following error will be reportedMissing required parameter "config" when instantiating "EasyWeChatFoundationApplication".
I saw that the instantiation of this class requires an array to be passed in as a configuration file, so I changed the code to the following:

<code>
    '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: 绝对路径!!!!
                    ],
    
                    //...
                ],
            ],
</code>

But still the same, Missing required parameter "config" when instantiating "EasyWeChatFoundationApplication".

So I want to ask, if I want to instantiate something like this that requires parameters, how do I pass in the parameters?

in bootstrap.php

<code>
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: 绝对路径!!!!
  ],

  //...
],
]);</code>

In the configuration file, configuring the parameters of construct is not supported. You need to configure container to tell Yii to inject the configuration information into contruct during instantiation.

Give you a DEMO

<code> 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;
    }</code>

You write like this

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn