Adding .env file support to a legacy PHP project

Photo by Stephen Crowley on Unsplash

It’s a modern day standard that all web applications store their platform specific configuration in environment variables. In the PHP world, this usually mean a .env file in the root of your project.

Most modern frameworks, such as Laravel and Symfony, come with built in support for a .env file. For older projects, which may have an outdated framework, or perhaps no framework, there may be no support at all. In some cases, you might simply prefer to use a more standard environment file over the way in which configuration is currently provided by your project.

An Easy Solution

As part of upgrading a very old web application to a more modern architecture, I created an open source package, dotenv-loader. This package provides very simple, automatic .env file support.

To install the dotenv-loader package, simply run the following Composer command from the root of your project.

composer require DivineOmega/dotenv-loader

If your project or its framework already uses Composer, then you’re done. If not, make sure you include vendor/autoload.php in any file you wish to access your .env file variables from.

You can now create a .env file in the root of your project, alongside your composer.json file and populate it as follows.

CUTTING_TOOL="carving knife"

You can then easily access these variables from within your PHP scripts. Here’s a example.

require 'vendor/autoload.php';echo ucfirst(getenv('MICE_QUANTITY')).' '.getenv('MICE_STATUS').' mice. ';
echo ucfirst(getenv('MICE_QUANTITY')).' '.getenv('MICE_STATUS').' mice. ';
echo '<br/>';echo 'See how they '.getenv('MICE_SPEED').'. ';
echo 'See how they '.getenv('MICE_SPEED').'. ';
echo '<br/>';echo 'They all ran after the farmer\'s wife,';echo '<br/>';echo 'Who cut off their tails with a '.getenv('CUTTING_TOOL').',';echo '<br/>';echo 'Did you ever see such a sight in your life,';echo '<br/>';echo 'As '.getenv('MICE_QUANTITY').' '.getenv('MICE_STATUS').' mice?';echo '<br/>';

Poor mice.

For more information about this package and additional documentation, see the dotenv-loader readme on GitHub.




🖥️ Full stack developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Super Backend Man — How to become an awesome backend developer

Unrestricted File Upload — P1

How to create a web app with Django.

The One Question to Ask When Considering Automating Accounts Payable

Hola People 🎭

String manipulation and Regular expression with exception Handling

How computer deals with floating point numbers

The unreasonable effectiveness of simple HTML

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jordan Hall

Jordan Hall

🖥️ Full stack developer

More from Medium

How to use Laravel’s default user table for Twill CMS authentication.

Back to Laravel Homestead from Sail / Docker

How to develop an HTML and JS source code viewer using PHP

How to create CRUD operation with CodeIgniter 4 and React JS