What is difference between use env('APP_ENV')
, config('app.env')
or App::environment()
to get app environment?
I know that the env('APP_ENV')
will to $_ENV
, config('app.env')
reads the configuration and App::environment()
is an abstraction of all. And in my opinion the advantage is even this. Abstraction.
I do not know if there are other differences, such as the level of performance or security
One thing to consider is perhaps the convenience factor of passing string to
app()->environment()
in order validate your current environment.In Short & up-to-date 2019:
- use env() only in config files
- use App::environment() for checking the environment (APP_ENV in .env).
- use config('app.var') for all other env variables, ex. config('app.debug')
- create own config files for your own ENV variables. Example:
In your .env:
example config app/myconfig.php
Access in your code:
Explanation & History:
I just felt over it. When you cache your config file, env() will (sometimes?) not work right. So what I found out:
From here: https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes
UPDATE:
env() calls work as long as you don't use php artisan config:cache. So it's very dangerous because it will often work while development but will fail on production. See upgrade guide: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0
UPDATE Laravel 5.6:
Laravel now recommends in its documentation to use
and describes that env() is just to retrieve values from .env in config files, like config('app.env') or config('app.debug').
In 12factor methodology application contains two types of configuration values:
./config/
folder. In this type we usually store some technical optimal/good values used in application which should not be changed by users over time e.g. optimal image compression level, connection timeout, session expiration time etc..env
file (but should not be stored in git repo, however.env.example
with example values with detail info can be stored in repo). In this type we store usually some important/protected values which depends on local environment e.g. passwords, debug mode, db address etc.Laravel proposes handy approach for this
config(...)
helper (so on this level programmer do not need to know which configuration value is internal and which is external)env(...)
helper e.g. in config/app.php'debug' => env('APP_DEBUG', false)
You have two equally good options
or
app()->environment() is actually used by Bugsnag, look in documentation here it says
If you are using the
config:cache
command during deployment, you must make sure that you are only calling theenv
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.Add an env configuration option to your
app.php
configuration file that looks like the following:More: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0