How to Define Custom ENV Variables in Laravel?

How to Define Custom ENV Variables in Laravel?

Written by Bobby Iliev on Apr 29th, 2021 Views Report Post

Introduction

If you have some experience with Linux, you are probably quite familiar with environment variables. In Linux, you could check the available environment variables with the printenv command. A way to define environment variables in Linux would be to use the export command followed by the variable that you want to define, for example: export name=DevDojo.

In this tutorial, you will learn a couple of ways of defining Laravel variables and accessing them in your application!

Prerequisites

Before you start, you would need to have a Laravel application up and running.

I will be using a DigitalOcean Ubuntu Droplet for this demo. If you wish, you can use my affiliate code to get free $100 DigitalOcean credit to spin up your own servers:

DigitalOcean Referral Badge

If you do not have that yet, you can follow the steps from this tutorial on how to do that:

Or you could use this awesome script to do the installation:

Defining a variable in .env

The .env file holds your env variables for your current environment. It is powered by the DotEnv Library.

As the .env file often holds sensitive information like API keys or database credentials, you should never commit it to Git and never push it to GitHub.

In order to define a new environment variable, you could use the following syntax: VAR_NAME=YOUR_VALUE. For example, let's say that you wanted to define your GitHub API key in there, the syntax would be the following:

GITHUB_API_KEY=your_api_key_here

In case that you have spaces in the value, it is best to use quotes:

NAME="Bobby Iliev"

Accessing the env variable with env()

Once you've specified your environment variable in your .env file, you could then retrieve it in your application with the following helper function:

env('NAME')

The env helper function allows you to pass a default value in case that the env variable is not defined or does not have a value specified in the .env file:

env('NAME', 'DevDojo')

In the above example, if the NAME env variable is not defined, it will take the default value of DevDojo.

Accessing the env variable with config()

If you are creating a new Laravel Package or if you are building some specific functionality, it is pretty handy to define different configuration values in a configuration file stored in the config folder.

For example, let's say that we are building functionality that consumes the DigitalOcean API. What we can do is create a new file inside the config folder called digitalocean.php:

config/digitalocean.php

The content of the fill will be a single return statement that returns an array with different env configuration values:

<?php
  
return [
    'do_key' => env('DIGITALOCEAN_API_KEY', 'default_value_here'),
];

With that we would also need to define the actual value of the DIGITALOCEAN_API_KEY variable inside the .env file just as we did in the previous step:

DIGITALOCEAN_API_KEY='your_digitalocean_api_key_here'

After that, by using the config helper method, you could access the do_key value with the following syntax:

config('digitalocean.do_key')

Rundown of the syntax:

  • config(''): First, we call the config helper
  • Then, we specify the name of the configuration file inside the config folder. As we named the file digitalocean.php we need to just use digitalocean
  • Finally separated with a dot we specify the value that we want to call

With that, you could also use the php artisan config:cache command to cache your env variables and give your website a slight speed boost.

Important thing to keep in mind is that if you run the config:cache command, you will only be able to use the env helper inside configuration files in the config folder. And also, you need to have the php artisan config:cache command executed on every deployment.

Conclusion

Now you know how to define custom variables in your Laravel application! For more information about the Laravel configuration, make sure to check out the official documentation here:

https://laravel.com/docs/8.x/configuration

If you are just getting started with Laravel, make sure to check out this introduction course here:

Introduction to Laravel

Comments (0)