Home

Awesome

Setup and work with .env files in Laravel from the command line

NOTE: This doesn't work with Laravel 5 since .env files were changed. This is for Laravel 4.X

Latest Stable Version Build Status Coverage Status License

Future features

This Laravel 4 package gives you a way to setup and work with your environment files within Laravel from the command line.

Installation

Install the package through Composer. Edit your project's composer.json file to require brunty/laravel-environment.

"require": {
	"brunty/laravel-environment": "0.*"
}

Next, update Composer from the Terminal:

composer update

Once this has completed, add the service provider to your service providers array in app/config/app.php

'Brunty\LaravelEnvironment\LaravelEnvironmentServiceProvider'

You should then be able to see the command within artisan

php artisan

Usage

Use the above command to setup and/or configure an environment file, it works with the optional --env flag to specify what environment the file should be for.

When using this command, it'll prompt you for the variable name first, then the value.

If you want to specify a multi-dimensional array of items, you can use dot notation:

db.host

With a value of foo

Would be put into the .env file under:

[
    'db'    =>  [
        'host'  =>  'foo'
    ]
]

The names you enter for your env variables will autocomplete with existing values you have in the file.

To finish setup, just hit enter without giving a name when the command prompts you for a name.

It'll then give you a table showing the values that will be written to the file and prompt you to confirm that you want to write these values.

Access environment variables

Previously, you may have accessed your environment variables with the $_ENV superglobal like so:

'key'   =>  $_ENV['ENV_VAR'],

This can cause undefined index errors if you don't have a file for your environment already setup (this package can create a file from blank if required) when using this way of accessing environment variables.

I would recommend that you reference your environment variables using:

'key'   =>  getenv('varname'),

Which will simply return false if the environment variable doesn't exist (any 'multi-dimensional' items can be access with dot notation in the same way you entered them)

Notes:

Example of an early version working

Example of an early version working