当前位置:Gxlcms > PHP教程 > yii2$app里面如何自定义组件

yii2$app里面如何自定义组件

时间:2021-07-01 10:21:17 帮助过:21人阅读

有个微信的第三方包,通过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'];试试看

人气教程排行