Using a .env file to configure PHP application has many advantages, especially if you are using Git to manage your project.

Why using env

Some information is never suitable to be stored in your code such as your database connection details. These information is sensitive and may vary in different location, like your development site and production site. Therefore, a separate file should contain all of this information and not to be included in the git tracking list.

Steps

To using env, you can use the package phpdotenv from vlucas by composer

composer require vlucas/phpdotenv

After the installation finished, create a file called .env and in root of your project. Open the .env with your preferred editor, you can typed down your own information using below format


DB_HOST=localhost
DB_NAME=database_name
DB_USER=root
DB_PASS=                        # for empty value
DB_DESC=

...

[ Read More ]