我为 paypal 创建了一个自定义服务提供商。 当我尝试从提供者内部访问配置时,它返回 null。
下面是我写的代码。
public function register()
{
$this->app->singleton(ApiContext::class, function($app){
$paypalConf = $app['config']->get('paypal');
$apiContext = new ApiContext(new OAuthTokenCredential(
$paypalConf['client_id'],$paypalConf['client_secret']
));
$apiContext->setConfig($paypalConf['settings']);
return $apiContext;
});
}
但它抛出以下错误
if (empty($credObj)) {
throw new PayPalInvalidCredentialException("Credential not found for " . ($userId ? $userId : " default user") .
". Please make sure your configuration/APIContext has credential information");
}
"Credential not found for default user. Please make sure your configuration/APIContext has credential information
这肯定是因为我的配置文件返回 null。
这是我的paypal.php
return [
'client_id' => env('PAYPAL_CLIENT_ID', ''),
'client_secret' => env('PAYPAL_SECRET', ''),
'settings' => [
'mode' => env('PAYPAL_MODE', ''),
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path().'/logs/paypal.log',
'log.LogLevel' => 'ERROR'
]];
并且 .env 文件中存在 PAYPAL 凭据。
我已经尝试清除配置并再次缓存它,但它没有用。
更新
似乎 register 方法在服务提供商中不起作用。 但是我已经在 config/app.php 中注册了提供者
最佳答案
没有更多信息......
清除配置缓存:php artisan config:clear
如果你有一个缓存的配置并且你添加任何东西到配置文件或环境,包括另一个服务提供者到config/app.php
,它不会被拾取因为它使用缓存您的配置版本。
您可以通过进入 tinker
并在更改任何配置文件或环境后请求这些配置值来验证此类内容。
关于php - 无法访问 ServiceProvider 中的 config() |拉维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503426/