Understanding the Laravel Configuration

Laravel offers us a great diversity possibilities out of box. Like as environments of production and development, and knowing about config is essential to start understanding how framework works.

It is often helpful to have different configuration values based on the environment where the application is running. For example, you may wish to use a different cache driver locally than you do on your production server.

By default the main configuration file is .env and it’s in main folder. How I told in a past post, Laravel uses the DotEnv Library to get these variables inside this file. The .env file is ignored by GIT because each environment can have a different configuration.

The basic configuration of Laravel are in .env file and under files at config directory.

About config files:

  • All files have comments about each variable.
  • All files returns a array with these values.

If I can advise you, I will recommend you to open the config files. It helps you to understand more about the framework.

If you open the file config/services.php you will see it:

Laravel have a helper function called config().

The config function gets the value of a configuration variable. The configuration values may be accessed using “dot” syntax, which includes the name of the file and the option you wish to access.

Because of this you can get these values under your application simply using it:

But look that these variable will be defined in your .env file, let’s see the default .env

In this example, the $default will be set to null because isn’t on our .env. To fix it you can add the variable in .env file like it.

Now your $default will return “myfirstapp.com

Another important helper related to configuration files is the env()

The env function gets the value of an environment variable or returns a default value:

And you can do even more! You can create your own config files, and the files can set variables getting values from .env or directly from the array.

Here’s a little example, firstly create the config/myconfig.php like below:

Now you can use these values in everywhere on your application.

See you soon 😉

Share this!




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *

*