欢迎各位兄弟 发布技术文章
这里的技术是共享的
php artisan config:cache
把配置文件缓存起来 就可以了 见下面 红字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 theconfig:cache
command during your deployment process, you should be sure that you are only calling theenv
function from within your configuration files.
'env' => env('APP_ENV')
Cache the config files using php artisan config:cache
Instead of calling env('APP_ENV');
use \Config::get('app.env');
to avoid the issue.
Use config:cache
for every deployment.
If you make requests fast enough some or all environment variables return null. To reproduce:
This bug occurs for me using WAMP 2.5 x64 (latest) on Windows 7. Haven't tested on *nix yet. |
For anyone else wondering why this happens: |
You mean |
So to be clear:
'env' => env('APP_ENV')
This is like death by 1001 papercuts, just one more thing to worry about. Why was this feature added |
I don't get why would you need to cache
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. |
I don't need to, I'm not asking why the feature was added, I'm asking why |
Hey @tylercubell, It is still happening in 5.2 or someone solve the issue? |
Hi, I'm encountering the same issue here, no clear solution yet ? |
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) |
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. |
Hi, when calling \Config::get('app.env'); I'm still getting local, although I changed the env to production. 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 |
Hi @decadence , Good luck. |
I am facing the same issue. And the sad part is, |
@milon |
@llioor 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. |
@liuyunzhuge in lumen you have to load the config file in
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. |
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 http://stackoverflow.com/questions/39046560/laravel-5-2-envapp-env-does-not-work-in-production |
What we should do if we are trying to use In my middleware I have something like this:
Should I add my custom variables inside of a config file somehow and then call them with another function that is not UPDATE: Yes, that solves the problem. I created
and now my middleware looks like this:
Finally clear your cache and rebuild it again. I find this cleaner than calling directly |
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. |
You should avoid calling |
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? |
What you can do is (re)load the 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
|
|