Laravel 5 - env() 总是 returns 空

Laravel 5 - env() always returns null

我试图找出为什么我的 env() 助手总是 returns null。这会导致麻烦,尤其是在 app.php 文件中,默认情况下 env() 助手被广泛使用。也许有什么神秘的服务器设置?

我的环境文件:

APP_ENV=production
APP_KEY=base64:mymagickey=
APP_DEBUG=false
APP_LOG_LEVEL=info
APP_URL=http://www.example.com

etc...

编辑 - 我试过以下:

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

当然,我正在使用这样的 env 助手:env('APP_ENV')

但是还是没有成功。奇怪的是,$_ENV php 变量包含 .env 文件中的每个变量。

这是一个“.env”已知错误,可以通过以下方式解决:

php artisan config:cache

五个最重要的命令,如果您的 Laravel 在 .env 或数据库文件夹中进行一些修改后或由于任何其他修改而没有按预期工作。 这是完整的解释: https://www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload

使用 \Config::get('app.env'); 而不是 env(APP_ENV);,因为您最终会遇到同样的错误,这对实时网站不利。

如果您想从您的 ENV 添加自定义变量,请进入您的配置应用并找到:

/*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

'env' => env('APP_ENV', 'production'),

在“'env' => env('APP_ENV', 'production'),”下添加一个新行,例如,它可以是:

/*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

'env' => env('APP_ENV', 'production'),
'key' => env('APP_KEY'),

您可以这样调用 "key" 变量:

\Config::get('app.key');

每当您向应用程序环境添加 "key" 等新变量时,您都需要使用 config:cache 来重置缓存。

希望这个命令能拯救你

php artisan config:clear

缓存配置后,

env(...) 功能将不起作用。 (从 laravel 5.2 开始到当前 5.7)

Laravel documentation

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

所以正确答案是

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

我引用了同一个 documentation

但为了快速解决这个问题:

php artisan config:clear

现在应该清楚为什么当您尝试 config:cache 时它没有帮助,即使它在缓存之前清除了配置。

以下命令对我有用。 我不小心清除了所有缓存文件,所以 env('something') 返回 null。

php artisan optimize:clear

我知道这是一个非常古老的话题,原因可能不一样。但是我也遇到了同样的问题。

原因是我在 .env 文件中遇到问题。这就是我在 .env

中得到的
TEST="e@1asa

请注意结尾引号丢失了。它应该是这样的,

TEST="e@1asa"

由于这个错误,我之后添加的所有 env 变量都返回 null。

甚至没有写入错误日志。因此,如果您遇到此错误尝试添加示例环境变量作为文件的第一个记录。如果它有效并且 记录最后不工作 .env 中可能有错误

将 .env 文件放在项目的根文件夹中

将这些添加到 app.php 文件。

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    echo $e;
}

如果接受的答案不能解决问题,请检查 .env 文件是否具有正确的读写权限。如果框架/库无法读取文件,则值将始终为空。我在我的一个 Lumen 项目中遇到过这个问题,但在更改文件权限后它起作用了。

只需 运行 在您的 Laravel 项目根目录下执行此命令

rm bootstrap/cache/config.php

phpartisanconfig:clear。 请 运行 此命令将清除配置缓存。 它的发生是因为配置文件被缓存,并且在缓存时值必须为空。

很遗憾,已接受的解决方案并未针对此问题提供合适的解决方案!

案例:由于Laravel 5.2,如果你直接调用env("key"),如果你执行php artisan config:cache作为最后一个命令,它总是return null

接受的答案说:执行php artisan config:clear作为解决问题的最后一个命令! 这不是解决方案!,因为我们需要始终执行 php artisan config:cache 作为最后一个命令..

那么解决方法是什么?

在这里我需要解释两种情况的解决方案:


案例 1(预定义键):如果您需要从 Laravel 项目创建的 .env 文件中获取值(默认 .env 文件), 或者通过你已经安装的第三方插件按照正确的安装步骤安装:

  1. 获取.env文件中列出的键名并在laravel_root/config文件夹的文件中搜索它(记住:config文件夹中的所有文件return的结果作为数组),例如我搜索了 APP_URL ,它由 Laravel 项目默认定义:

正如你在这里所说的,我在两个配置文件中发现了两个结果,一个在 filesystem.php 中,另一个在 app.php 中,因此要在任何 php 中获取这些值中的任何一个(类 或查看文件,...等)在您的 laravel 项目中,只需这样称呼它:

  • 对于我们在 app.php 文件中找到的结果,通过以下方式获取它的值:

config('app.env');

此处 app 是 php 文件的名称,env 是 return 作为 app.php 文件结果编辑的数组的键。

  • 对于我们在 filesystems.php 文件中找到的结果,通过以下方式获取它的值:

config('filesystems.public.url');

这里filesystems是php文件名,public是密钥(1级) url 是数组的键(第 2 级),return 作为 filesystems.php 文件的结果编辑。

Summary of this case: if you need get value of APP_URL in any php file of Laravel project you can get it by call config('app.env') function, whether the last command executed is php artisan config:clear or php artisan config:cache, It doesn't matter at all.


案例 2(非预定义密钥 - 如果您需要生成新的自定义环境密钥):请参阅此答案以获取解释:

记得检查您的 .env 文件是否有重复的变量声明

STRIPE_PUBLISHABLE_KEY="why-wont-this-key-get-outputted"

#...

# Because at the bottom of the .env file it is unset like this
STRIPE_PUBLISHABLE_KEY=

尝试以下命令后

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

如果您仍然从 env('APP_ENV') 得到 null,那么试试这个 app()->environment()。希望这不会 return null。这对我有用