gpt4 book ai didi

Yii2 参数访问公共(public)目录中的本地配置文件

转载 作者:行者123 更新时间:2023-12-04 07:52:15 31 4
gpt4 key购买 nike

我正在使用 Yii2高级模板,
我要访问params.phpmain-local.php文件,
我这样称呼:

主本地.php:

'mailer' => [
'class' => 'myClass',
'apikey' => \Yii::$app->params['mandrill_api_key'],
'viewPath' => '@common/mail',
],

我已经存储了这个 mandrill_api_keyparams.php
参数.php:
<?php
return [
'adminEmail' => 'admin@example.com',
'supportEmail' => 'support@example.com',
'user.passwordResetTokenExpire' => 3600,
'mandrill_api_key' => 'mykey'
];

我收到此错误:

Notice: Trying to get property of non-object in C:\xampp\htdocs\myproject\common\config\main-local.php on line 25



我应该怎么做才能访问这些参数?

最佳答案

在实例化应用程序之前读取配置文件,如 request lifecycle 中所述。 :

  1. A user makes a request to the entry script web/index.php.
  2. The entry script loads the application configuration and creates an application instance to handle the request.
  3. The application resolves the requested route with the help of the request application component.
  4. ...


因此 \Yii::$app尚不存在因此错误。我建议将您的 api_key 定义移至 main-local.php配置,这样就不会混淆它的设置位置:
'mailer' => [
'class' => 'myClass',
'apikey' => 'actual api key',
'viewPath' => '@common/mail',
],

或者,您可以使用 Yii2 的 dependancy injection container设置 apikey在应用程序的入口脚本中:
...
$app = new yii\web\Application($config);
\Yii::$container->set('\fully\qualified\myClass', [
'apikey' => \Yii::$app->params['mandrill_api_key'],
]);
$app->run();

关于Yii2 参数访问公共(public)目录中的本地配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689230/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com