Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between use env('APP_ENV'), config('app.env') or App::environment() to get app environment?

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

like image 366
Miguel Borges Avatar asked Oct 13 '16 16:10

Miguel Borges


People also ask

What is env config?

You configure your environment by setting environment variables and creating or modifying files that relate to the environment variables. You can control whether environment variables are set at the environment level, for a specific user, or for a database session.

What is use of .env in Laravel?

Laravel's default . env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.

Where is .env file in Laravel?

In your main Laravel folder you should have . env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env('KEY'). Don't confuse it with .

What is config in Laravel?

Laravel configuration allows you to define per-environment configuration with the excellent vlucas/phpdotenv package. If you are new to Laravel, you might not yet know how you can create your configuration files in your projects and a few other helpful things that will help you master configuration.


2 Answers

In Short & up-to-date 2022:

  • 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:

    MY_VALUE=foo

example config app/myconfig.php

return [     'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env ]; 

Access in your code:

config('myconfig.myvalue') // will result in 'foo' 

Explanation & History:

I just felt over it. When you cache your config file, env() will (sometimes?) not work right. So what I found out:

  1. Laravel recommends only to use env() within the config files. Use the config() helper in your code instead of env(). For example you can call config('app.env') in your code.
  2. When you use php artisan config:cache all the configuration strings are cached by the framework and any changes you make to your .env file will not be active until you run the php artisan config:cache command again.

From this article on Laracast:

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

Caching And Env

If you are using the config:cache command during deployment, you must make sure that you are only calling the env 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.

UPDATE Laravel 5.6:
Laravel now recommends in its documentation to use

$environment = App::environment();  // or check on an array of environments: if (App::environment(['local', 'staging'])) {     // The environment is either local OR staging... } 

and describes that env() is just to retrieve values from .env in config files, like config('app.env') or config('app.debug').

like image 51
ndberg Avatar answered Sep 18 '22 07:09

ndberg


You have two equally good options

if (\App::environment('production')) {...} 

or

if (app()->environment('production')) {...} 

app()->environment() is actually used by Bugsnag, look in documentation here it says

By default, we’ll automatically detect the app environment by calling the environment() function on Laravel’s application instance.

Now, differences:

1) env(...) function returns null after caching config. It happens on production a lot.

2) you can change config parameters inside unit tests, it gives you flexibility while testing.

like image 44
Yevgeniy Afanasyev Avatar answered Sep 18 '22 07:09

Yevgeniy Afanasyev