Laravel .env变量始终返回null

Erd*_*dem 7 php laravel laravel-5

我将此变量添加到.env文件中

STRIPE_SECRET = A12345

我想使用routes/web.php转储变量

<?php
dd(env('STRIPE_SECRET'));
Run Code Online (Sandbox Code Playgroud)

但看起来它总是返回null.

更新:更新.env文件.我只删除了DB_PASSWORD.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:08txDXXatyYsP5WQ4ECz35Q7OyBEe8Vgb/zK5fZsHik=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_LOCALE=tr
APP_LC_ALL=tr_TR.utf8

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gunluk
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=03ac580c85842d
MAIL_PASSWORD=1d6d902d296942
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

STRIPE=a123
STRIPE_SECRET=a12345
Run Code Online (Sandbox Code Playgroud)

RAU*_*MAR 10

首先,文件中没有STRIPE_SECRET内容.env(按编辑问题之前).所以请确保您.env必须拥有此变量.您应该通过以相同的顺序执行以下命令来清除配置缓存

php artisan config:cache
php artisan config:clear 
Run Code Online (Sandbox Code Playgroud)

Laravel缓存配置文件,以便更快地执行.因此,每次更改服务器上的配置文件时,都应清除缓存.

此外,您还可以运行这些命令以清除其他缓存

php artisan cache:clear   //for clearing the cache
php artisan view:clear    //for clearing the compiled views
php artisan route:clear   //for clearing the routes cache
Run Code Online (Sandbox Code Playgroud)

您还可以为这些命令创建路由,并从代码中调用命令

Route::get('/cache-clear', function() {
    $exitCode = Artisan::call('cache:clear');
    echo "Cache Cleard: ".$exitCode;
});

Route::get('/view-clear', function() {
    $exitCode = Artisan::call('view:clear');
    echo "View Cleard: ".$exitCode;
});

Route::get('/route-cache', function() {
    $exitCode = Artisan::call('route:cache');
    echo "Route Cached: ".$exitCode;
});

Route::get('/route-clear', function() {
    $exitCode = Artisan::call('route:clear');
    echo "Route Cache Cleared: ".$exitCode;
});

Route::get('/config-cache', function() {
    $exitCode = Artisan::call('config:cache');
    echo "Config Cached: ".$exitCode;
});

Route::get('/config-clear', function() {
    $exitCode = Artisan::call('config:clear');
    echo "Config Cache Cleared: ".$exitCode;
});
Run Code Online (Sandbox Code Playgroud)


Des*_*901 10

您遇到问题的主要原因是您正在缓存您的配置.在运行时,php artisan config:cache您将配置存储在缓存中,并且Laravel下次启动时将无法读取该.env文件,因为它检测到配置已存储在缓存中.环境文件应仅用于设置配置文件,然后访问您要查找的值,您应该只使用该config方法.

假设您拥有config/stripe.php包含此内容的文件:

<?php

return [
    'secret' => env('STRIPE_SECRET', '')
];
Run Code Online (Sandbox Code Playgroud)

一旦您通过应用程序代码php artisan config:cache仅使用语法运行访问此值config('stripe.secret').每次更新配置文件时,您.env都需要php artisan config:cache再次运行.


Bil*_*med 5

如果文件中有 STRIPE="a12345".env文件,或者.env文件或config文件中有任何更改,则请遵循以下步骤

还有一件事,以逗号的形式写变量值 STRIPE="a12345"

首先运行这些命令

 1. php artisan config:clear
 2. php artisan cache:clear
 3. composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

最后使用此命令获取变量

dd(env('STRIPE'));
Run Code Online (Sandbox Code Playgroud)

这对我有用

还有1个愚蠢的建议:重新启动服务器

我已经添加了所有可能的解决方案