Environment and configuration variables in Laravel

Author
By Darío Rivera
Posted On in Laravel

Configuration variables are used to define values that are global and that can change depending on whether the application is installed in one environment or another. Laravel has a structured configuration system that is composed of all the files you can find within the /config folder, the .env file (DotEnv PHP by Vance Lucas), and some helpers such as config and env.

Variables in the .env file

To define variables in the .env file, simply use the following syntax:

NAME=VALUE

If the value is a string separated by spaces, double quotes must be used.

NAME="VALUE WITH SPACES"

The helper function used to obtain the values of the variables in the .env file is env, which takes the defined variable name as its first parameter and a default value as its second parameter in case the variable does not exist. This helper, which is nothing more than a PHP function defined globally, can be used in configuration files or in any controller or model of the application.

env('NAME', 'default value');

Variables in configuration files

Variables in configuration files do not use DotEnv, so they are simple PHP files that return an array with key-value pairs. However, it is very common in these files to reference values in the configuration file .env. For example, let's look at the definition of the application name in the config/app.php file.

return [

    /*
    |--------------------------------------------------------------------------
    | Application Name
    |--------------------------------------------------------------------------
    |
    | This value is the name of your application. This value is used when the
    | framework needs to place the application's name in a notification or
    | any other location as required by the application or its packages.
    |
    */

    'name' => env('APP_NAME', 'Laravel'),
    ...

As you can see, the name value is clearly defined that points to the value that the APP_NAME variable would have in the .env file. This configuration scheme serves to link the configuration variables of /config with the particular values of the .env file, as it is more convenient to edit a file that uses DotEnv than a PHP file.

The helper function used to obtain the values of the variables in the configuration files is config, which takes the path of the configuration file as its first parameter using the dot character as the directory separator. Both this helper and env can be used in any controller or model of the application. Next, we see an example of how to obtain the application name from the configuration.

$appName = config('app.name');

An advantage of using the variables directly from the configuration files and not from the .env file is that the value of a variable can be modified at runtime.

config(['app.timezone' => 'America/Chicago']);

As a final recommendation, it should always be kept in mind that these variables can be overwritten by server or system-level variables. See you soon!.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.