欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

刷新的时候 有时 env('变量') 为空 null 怎么办呢 laravel refresh page env null 有大用 有大用

 

可以与 /node-admin/9941 结合起来看

使用 先 php artisan cache:clear (php artisan config:clear) 再 php artisan config:cache  把配置文件缓存起来 就可以了 见下面 红字

好像不行 我就在 /config/services.php 里进行 env('变量')

生成 bootstrap/cache/config.php 后    /config/services.php 里的变量 就在 bootstrap/cache/config.php 里了

env 函数 在 执行了 php artisan config:cache 后 只能 在 config/目录下的 配置文件里执行

Configuration Caching

To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which will be loaded quickly by the framework.

You should typically run the php artisan config:cache command as part of your production deployment routine. The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development.

 

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.

 



[5.0] Environment variables sometimes return null #8191


So to be clear:
  1. In, say, /app/config/app.php add:
    'env' => env('APP_ENV')
  1. Cache the config files using php artisan config:cache

  2. Instead of calling env('APP_ENV'); use \Config::get('app.env'); to avoid the issue.

  3. Use config:cache for every deployment.

 

 
 Closed
tylercubell opened this issue on 27 Mar 2015 · 36 comments

Comments

 

 
Projects
None yet
20 participants
@tylercubell@GrahamCampbell@crynobone@llioor@P4Thi0ut@toby1kenobi@aruncnt@JJJJJJJerk@hugobluefuse@decadence@milon@liuyunzhuge@hoangdangal@jayc667@dmarman@zackify@brexis@thinkspill@phpfalcon@caneco
@tylercubell
 

tylercubell commented on 27 Mar 2015 • edited

 

 

If you make requests fast enough some or all environment variables return null.

To reproduce:

  1. Install fresh L5.
  2. Edit /app/Http/Controllers/WelcomeController.php
  3. Add dd(env('APP_ENV')); in the index function.
  4. Make a bunch of requests in quick succession, i.e. hold down F5 for 2 seconds in a browser.

This bug occurs for me using WAMP 2.5 x64 (latest) on Windows 7. Haven't tested on *nix yet.

 

 GrahamCampbell closed this on 27 Mar 2015

 

 
 
@GrahamCampbell
 
Member

GrahamCampbell commented on 27 Mar 2015

 

 

Duplicate. This is a known issue with php. We've discussed it here before. Feel free to search for the issue.

 
@tylercubell
 

tylercubell commented on 27 Mar 2015

 

 

For anyone else wondering why this happens:

866a8cf#commitcomment-9767647

#7354

#8187

 
@tylercubell
 

tylercubell commented on 27 Mar 2015

 

 

Is there anyone working on a fix at PHP? I can't rely on the whole .env system if it's going to randomly crap out on me.

Is the only workaround to hard-code values into config files right now?

 
@crynobone
 
Contributor

crynobone commented on 27 Mar 2015

 

 

Another workaround is to use php artisan config:cache for your production environment.

 
@tylercubell
 

tylercubell commented on 27 Mar 2015

 

 

The artisan command didn't work for me. The same behavior still happens.

 
@crynobone
 
Contributor

crynobone commented on 27 Mar 2015

 

 

The artisan command didn't work for me. The same behavior still happens.

You mean dd(env('APP_ENV')); behaviour? Obviously that expected which why I said "for your productionenvironment"https://github.com/laravel/framework/blob/5.0/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php#L28

 
@tylercubell
 

tylercubell commented on 27 Mar 2015

 

 

So to be clear:

  1. In, say, /app/config/app.php add:
    'env' => env('APP_ENV')
  1. Cache the config files using php artisan config:cache

  2. Instead of calling env('APP_ENV'); use \Config::get('app.env'); to avoid the issue.

  3. Use config:cache for every deployment.

This is like death by 1001 papercuts, just one more thing to worry about. Why was this feature added before the PHP bug was fixed? I don't get it...

 
@crynobone
 
Contributor

crynobone commented on 27 Mar 2015

 

 

I don't get why would you need to cache APP_ENV, you cache other value such as DB_USERNAMEQUEUE_DRIVER etc. This way your app remain working all the time as this code is already resolved in the cached config.php.

Why was this feature added

http://12factor.net/config

Why not use PHP (just like Laravel 4)?

Well, if that the case, you need a new "duplicate" configuration for your none PHP app, if you want to use Python or Go for some of the background/queue process.

 
@tylercubell
 

tylercubell commented on 27 Mar 2015

 

 

I don't need to, APP_ENV was just an example. I'm caching other things.

I'm not asking why the feature was added, I'm asking why now. Why before the PHP bug is fixed?

 

 janhartigan referenced this issue in vlucas/phpdotenv on 28 Mar 2015

 

 Closed

Changing process environment unsafe on multithreaded servers #76

 

 
@llioor
 

llioor commented on 26 Mar 2016

 

 

Hey @tylercubell,
Is there any news about this issue?
Right now I'm setting the "production values" as default in my config files like: (db_password, *_production_password_)

It is still happening in 5.2 or someone solve the issue?
Thanks.

 
@P4Thi0ut
 

P4Thi0ut commented on 30 Mar 2016

 

 

Hi, I'm encountering the same issue here, no clear solution yet ?
I still try to believe that Laravel is a great framework, having to worry about such
issue is kind of unexpected and displeasing.

 

 misog referenced this issue on 7 Jun 2016

 

 Closed

Do something with dotenv finally #13906

 

 
@toby1kenobi
 

toby1kenobi commented on 21 Jun 2016

 

 

Please can the docs be updated, to reflect the requirement to use config:cache?! I just lost a few hours on trying to understand why env() was behaving unpredictably, which seems particularly futile now I discover it's been discussed here a few times.

 
@GrahamCampbell
 
Member

GrahamCampbell commented on 21 Jun 2016

 

 

This is documented in our upgrading guide. If there are any other places you'd like to see this in the docs, we'd be very grateful if you could send a PR please. :)

 
@toby1kenobi
 

toby1kenobi commented on 21 Jun 2016

 

 

Thanks for the quick response! So wait, is the docs site in Gitbhub, is that what you mean?

This page https://laravel.com/docs/5.2/configuration#determining-the-current-environment makes it sound like using .env would be a great thing to do, but even in dev I'm seeing it toggle between the values I want and null (where, presumably, default values don't exist)

 
@llioor
 

llioor commented on 22 Jun 2016

 

 

From reading "Do something with dotenv finally #13906" I understand that there is not a real solution for it on Apache and what you need to do is to run: "php artisan config:cache" in commend. I think it's a MUST part in starting laravel project docs.

Thank you @GrahamCampbell for sharing with us the commend.

 
@aruncnt
 

aruncnt commented on 25 Jun 2016

 

 

same issue with me

 
@JJJJJJJerk
 

JJJJJJJerk commented on 23 Aug 2016

 

 

upgrade my php to 7.0.10 function env get null value

 
@GrahamCampbell
 
Member

GrahamCampbell commented on 23 Aug 2016

 

 

This is a known issue with PHP. Make sure you run php artisan config:cache and never read env variables from outside the config files.

 
@JJJJJJJerk
 

JJJJJJJerk commented on 23 Aug 2016 • edited

 

 

run php artisan config:clear will fix the env null value drama

 
@GrahamCampbell
 
Member

GrahamCampbell commented on 23 Aug 2016 • edited

 

 

Not clear, but cache.

 
@hugobluefuse
 

hugobluefuse commented on 30 Sep 2016 • edited

 

 

Hi,

when calling \Config::get('app.env'); I'm still getting local, although I changed the env to production.
env(APP_ENV) returns null.

I'm using a thread safe php installation. Do I need to get the unsafe version?

PS: Workaround - get App:environment() from within App and pass it to the view

 
@decadence
 
Contributor

decadence commented on 11 Oct 2016

 

 

php artisan config:cache works for me on Windows 10, PHP 5.6, Apache 2.4.
Is this bug for PHP, Windows or Apache?

 
@llioor
 

llioor commented on 12 Oct 2016 • edited

 

 

Hi @decadence ,
It is not a PHP/Windows/Apache BUG, it is a ".env" known bug which can be solved with:
php artisan config:cache as you said.

Good luck.

 

 yajra referenced this issue in yajra/laravel-oci8 on 12 Oct 2016

 

 Closed

Very Rare ora-24415 missing or null username on refresh #174

 

 
@milon
 

milon commented on 9 Nov 2016 • edited

 

 

I am facing the same issue. And the sad part is, php artisan config:cache can't fix the issue. In my development environment(Mac OS sierra, php 7.0.12, laravel 5.3) if works fine, but in my production server(Ubuntu 14.04.4, php 7.0.11, laravel 5.3), it is not working. Any solution?

 

 milon referenced this issue on 9 Nov 2016

 

 Closed

config('value') is not working on cron #16333

 

 
@llioor
 

llioor commented on 9 Nov 2016

 

 

@milon
yes... try to do
php artisan cache:clear
and then
php artisan config:cache

 
@milon
 

milon commented on 9 Nov 2016

 

 

@llioor php artisan config:cache first calls the config:clear commad.

    public function fire()
    {
        $this->call('config:clear');

        $config = $this->getFreshConfiguration();

        $this->files->put(
            $this->laravel->getCachedConfigPath(), '<?php return '.var_export($config, true).';'.PHP_EOL
        );

        $this->info('Configuration cached successfully!');
    }

Anyway, I tried it as well. No luck.

 

 crynobone referenced this issue in laravel/internals on 23 Dec 2016

 

 Open

Throw warning on invalid config #250

 

 
@liuyunzhuge
 

liuyunzhuge commented on 16 Feb

 

 

How can I solve this problem with lumen ?

 
@milon
 

milon commented on 17 Feb

 

 

@liuyunzhuge in lumen you have to load the config file in bootstrap/app.php file like this-

$app->configure('config_file_name');

if you already do that, then comment the line and clear cache. then uncomment it again. I don't know why, but it worked for me.

 
@hoangdangal
 

hoangdangal commented on 20 Feb

 

 

run this :
php artisan config:clear

 
@jayc667
 

jayc667 commented on 10 Mar

 

 

I recently had this issue. it was fixed after I restarted the server.

Here is another solution I got from S.O

php artisan config:clear did the trick.

php artisan config:clear
php artisan cache:clear
service apache2 restart (just in case)

http://stackoverflow.com/questions/39046560/laravel-5-2-envapp-env-does-not-work-in-production

 
@dmarman
 

dmarman commented on 20 Mar • edited

 

 

What we should do if we are trying to use env() outside the config files?

In my middleware I have something like this:

public function handle($request, Closure $next)
    {
        if($request->header('api-token') != env('WATAPE_KEY')){
            abort(400);
        }
        
        return $next($request);
    }

Should I add my custom variables inside of a config file somehow and then call them with another function that is not env()?

UPDATE:

Yes, that solves the problem.

I created config/watape.php

<?php

return  [

    'api_key' => env('WATAPE_KEY'),
    
];

and now my middleware looks like this:

public function handle($request, Closure $next)
    {
        if($request->header('api-token') != config('watape.api_key')){
            abort(400);
        }
        
        return $next($request);
    }

Finally clear your cache and rebuild it again. I find this cleaner than calling directly env()

 
@zackify
 

zackify commented on 26 Apr

 

 

As someone not using laravel that often, it would have been nice for the first comment to mention how you should never use env variables directly and instead reference them in config files.

 

 ristaumedia referenced this issue in imanghafoori1/laravel-widgetize on 2 May

 

 Closed

Environment variables return null #5

 

 
@brexis
 

brexis commented on 10 May

 

 

You should avoid calling env()in your application. In some environments like testing, the env() function returns null. A good practice on laravel is to put variables in a config file and to use config() function instead of env() https://laravel.com/docs/5.4/configuration#configuration-caching. You can also cache your config files with php artisan config:cache to speed up your application.

 

@jakus1 jakus1 added a commit to jakus1/opus that referenced this issue on 13 May

 

@jakus1 jakus1 referenced this issue in zeeshanu/opus on 13 May

 

 Merged

Removing the env() function #85

 

 

@zeeshanu zeeshanu added a commit to zeeshanu/opus that referenced this issue on 15 May

 

thinkspill commented on 5 Jun

 

 

I ran into this issue when configuring Monolog in bootstrap/app.php. I was wanting to use different logging configurations depending on APP_ENV. Is there a recommended way to do this? Can Monolog be configured further down the line once APP_ENV is available to check?

 

 degan6 referenced this issue in monicahq/monica on 9 Jun

 

 Merged

Moved all instances of env out of app #158

 

 
@phpfalcon
 

phpfalcon commented on 13 Jun

 

 

don't use php artisan serve

 
@caneco
 

caneco commented 21 days ago

 

 

What you can do is (re)load the .env content. This will allow you to have access even during production and with your cached config file.

echo 'APP_NAME='.env('APP_NAME').PHP_EOL; // outputs `null`
with(new \Dotenv\Dotenv(base_path()))->load();
echo 'APP_NAME='.env('APP_NAME').PHP_EOL; // outputs your APP_NAME


环境变量有时返回null # 8191
关闭
tylercubell打开这问题 2015年3月26日·36评论

评论


@tylercubell
 

tylercubell 评论2015年3月26日编辑

如果你提出要求的足够快一些或所有环境变量返回null。

重现:

  1. 安装新的L5。
  2. 编辑/应用程序/ HTTP /控制器/ welcomecontroller.php
  3. 添加DD(env('app_env '));在指数函数。
  4. 让一串连续快速的要求,即按住F5在浏览器2秒。

这个错误发生我使用WAMP 2.5 x64的Windows 7(最新)。没有测试在*nix吗。

 
 
@GrahamCampbell

重复。这是一个已知的问题与PHP。我们已经讨论了它在这里。随意搜索问题。

 
@tylercubell

任何人想知道为什么会这样:

866a8cf# commitcomment-9767647

# 7354

# 8187

 
@tylercubell

有人工作在PHP修复?我可以不依赖于整个环境系统,如果它要随机躲避我。

唯一的办法是硬编码的值为配置文件吗?

 
@crynobone
 
贡献者

crynobone 评论2015年3月27日

另一个解决方法是使用PHP缓存配置:工匠为你的生产环境


普通分类: