'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'];试试看
