From the course: Laravel Essential Training

Understand Laravel configuration - Laravel Tutorial

From the course: Laravel Essential Training

Understand Laravel configuration

Every application has a bunch of configuration values that need to be accessed from different files all across the application, like the name of the app, database credentials, mail service credentials, and so on. To help manage all these environment variables, Laravel has a .env file at the root of the application. These are some app-related variables like name, environment, application key, which is unique to each project, then the app debug value, which determines how much of the error information should be displayed to the user. In the local environment, this is set to true by default because the error information is helpful for the developer to debug, but in production, this value should be set to false, otherwise, you risk exposing sensitive data. App URL is the base URL. In production, this would contain your domain name. Here is a set of database credentials including the type of connection, username, password. If you're using version control like git, you need to note that this file should not be committed to your repository. You have to add this file to gitignore. It's not added here because I'm using Codespaces and the environment file is very much needed. But if you install a new Laravel application in your local development environment, you will see that this .env file is already added in gitignore. This is not only for security reasons, it's also because each person in your team might have a different set of configuration values and your production server also, which is why we have a .env.example file which has all the variable names similar to .env file without the values or with default values. This file is committed to the repository, and everybody on the team can use it to create or edit their own .env files. So it's important to make changes to this example file, too, that is, add all of these new variables whenever you add them to your .env file. Now, the values of these .env file are retrieved from various files within the config directory. Look at database.php, for example. The env helper method returns the value of this key from the .env file. The second argument value is used as a default value in case the key is not found. Now, when we have to access these environment variables, we should not do this directly using the env function. Instead, we should use them through the configuration. For example, if we wish to access the app name, let's say in our welcome blade view, open welcome.blade.php, and let's say we want to replace this title with our app name. You can do so using the config helper method using the dot syntax. So our app name will be app.name. You can check that right here. If you go to app.php, the application name is a variable in the app.php file. So that's how you access all of these environment variables using the config method. Let's save this file, change our app name here to maybe NewApp, or something like that, and go to the browser, refresh. Notice that this has changed. So I encourage you to browse the contents of all the config files here and get a better hold on what are the configurations available in Laravel.

Contents