无法读取 laravel 5.4 中的新配置变量

问题描述 投票:0回答:3

我在 laravel 中的

config/app.php
文件中添加了一个新的配置变量,如下所示

'foo'  => 'pass'

但是当我尝试使用

config('app.foo')
读取它时,我不断得到
null
,而其他变量返回正确的值。我需要做什么?

php laravel config laravel-5.4
3个回答
27
投票

您使用的语法是正确的,请尝试使用以下命令清除配置缓存:

php artisan config:clear

4
投票

每当您更改配置时,您都必须清除旧缓存

Laravel 在缓存中保存一些常用设置,例如缓存文件夹中的配置、包和服务。如果您在 config/app.php 等中再次遇到配置或类未找到问题,那么您应该查看您的 缓存配置文件 (/laravel_project/bootstrap/cache/config.php) 来检查是否需要可用或不可用。

有时,当您在虚拟主机上工作时,路由或视图不会呈现更改,那么您还需要清除该部分的缓存。

为此,我们必须运行多个命令,例如:

 php artisan cache:clear
 php artisan view:clear
 php artisan route:clear
 php artisan config:clear

但是每天写这些命令是一件烦人的事情。所以我为它创建了一个包。 https://github.com/afrazahmmad/clear-all-cached-data。安装并运行:

php artisan clear:data

它将自动运行以上所有命令。


0
投票

如何修复读取配置或环境变量返回空

1-写入config/app.php

返回[

// project varaible
"FCM_serverkey" =>  'AAAA*************',

2- 在 .env 文件中写入相同的密钥 FCM_serverkey=AAAA****************

3- 从两个地方返回代码。写入键的格式为:“app”。 + 键 //读取配置 $apiKey = getenv('FCM_serverkey'); if( $apiKey == null ) { $apiKey = config('app.FCM_serverkey'); }

4- 将新代码拉取到服务器,然后写入清除缓存配置: php artisan 配置:清除

© www.soinside.com 2019 - 2024. All rights reserved.