doushu8260 2017-09-23 16:38
浏览 179
已采纳

Laravel .env变量始终返回null

I added this variable to .env file

STRIPE_SECRET=a12345

I would like to dump the variable using routes/web.php

<?php
dd(env('STRIPE_SECRET'));

But it looks like it always returns null.

Update : Updated .env file. I only removed 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
  • 写回答

6条回答 默认 最新

  • doukanzhuo4297 2017-09-26 11:05
    关注

    The main reason upon your issue is that you are caching your configuration. When running php artisan config:cache you're storing your configuration in your cache, and the next time Laravel will boot up it won't read the .env file because it detects that the configuration has been stored in the cache. Environment file should be used only to setup configuration files and then to access the value you're looking for you should use ONLY the config method.

    Let's assume that you have the file config/stripe.php that consists of this content:

    <?php
    
    return [
        'secret' => env('STRIPE_SECRET', '')
    ];
    

    Once you run php artisan config:cache access this value using ONLY the syntax config('stripe.secret') through your application code. Every time you update your config files and your .env you need to run php artisan config:cache again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题