Using .env for your local build
This was one of the best changes to Laravel from version 4 and 5 in my opinion. When I was doing Ruby on Rails work, it also had this feature, and this is the key to help create an application that falls in the Twelve Factor App workflow. You will learn how to use this file for setting keys to some recipes later on in the book. In this example, we will start using it to set up our database.
For the rest of this book, I will use PHPStorm for my editor, which helped me a ton to explore Laravel and PHP code when I first started. Make sure your editor has plugins to easily click and explore classes.
Getting ready
When you installed Laravel, it copied the .env
file into place. So, just open your editor of choice and open the application directory.
How to do it...
- Open
.env
in your editor. - Alter the file as follows, so the database name and the URL match what we would put in our Homestead setup file:
How it works...
First of all, this is a hidden file. The . in front of it makes it hard to see in File Managers and even the command line. When at the command line, ls -a *
is how to show this hidden file. Most code editors or IDEs will show you these.
Also note that Laravel comes with a .gitignore
file that includes this file:
We will have to consider the addition or changes of any settings in env
, as we push this application to Production for everyone to see when we are done. I will cover this more in Chapter 10, Deploying Your App.
So, what did we change in this file? Most of what you see was already there; we just set two things:
DB_DATABASE=recipes_local
APP_URL=https://recipes.dev
This is it, really! If you look back, this is what we set in the Homestead.yml
file. You can see what we called the database and domain name. So now we need to tell our application what database table to use and which Homestead is made for us. Yeah, for Homestead!
See also
- The 12 Factor App: http://12factor.net/
- PHPStorm: https://www.jetbrains.com/
- DotEnv (library that handles the .env file): https://packagist.org/packages/vlucas/phpdotenv