Laravel How to Get All env Variables Example Tutorial

Reading Time: 3 minutes
1,214 Views

Inside this article we will see the concept i.e Laravel 9 How to Get All env Variables Example Tutorial. Article contains the classified information about How to get all env variables in laravel.

If you are looking for a solution i.e Get all environment variables in laravel application then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

A environment variable is a variable whoes value is set before the program runs. A . env file or dotenv file is a simple text configuration file for controlling your Applications environment constants.

Read More: How To Work with cURL DELETE Request in Codeigniter 4

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Controller

Open project into terminal and run this command.

$ php artisan make:controller DataController

It will create a DataController.php file inside /app/Http/Controllers folder.

Read More: How To Work with cURL PUT Request in Codeigniter 4

Get All Environment Variables

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        $allEnvVariables = $_ENV;

        echo "<pre>";
        print_r($allEnvVariables);
    }
}

Here, It will list all environment variables.

Output

Get Specific Environment Variable

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        $allEnvVariables = $_ENV;

        echo $allEnvVariables['APP_NAME'];
    }
}

Here, It will print the value of APP_NAME environment variable value.

Read More: How To Work with cURL POST Request in Codeigniter 4

We hope this article helped you to learn Laravel 9 How to Get All env Variables Example Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

.