How to get env.php variables in Magento 2 inside php

How to get env.php variables in Magento 2 inside php

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get env.php variables in Magento 2 inside php

Accessing Magento 2 env.php Variables Inside PHP

As a senior developer working on Magento 2 projects, I often need to access environment configuration variables defined in app/etc/env.php directly from within my custom PHP code. This is particularly useful when you want to use environment-specific settings for your application logic.

Understanding the Magento 2 Environment File

The env.php file is a PHP file that stores the configuration parameters for your Magento 2 installation. These parameters are set during the installation process or via the command line using commands like bin/magento setup:config:set.

The structure of this file looks something like this:

return [
    'backend' => [
        'frontName' => 'admin'
    ],
    'install' => [
        'date' => 'Mon, 01 Jan 2018 00:00:00 UTC',
        ...
    ],
    'session' => [
        'save' => 'db'
    ],
    // Other configuration sections...
];

Accessing env.php Variables from PHP

To access these variables directly from your custom PHP code, you first need to include the app/bootstrap.php file. This file provides the necessary autoloading and environment setup for Magento 2.

Here’s an example of how you can do this:

<?php
// Include the bootstrap file
require_once 'app/bootstrap.php';

// Access the env.php variables
$envConfig = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ProductMetadataInterface')->getEnvironment();

// Now you can access the variables within $envConfig
echo $envConfig['db']['host'];

Using Magento’s ObjectManager

In this example, we’re using Magento’s ObjectManager to retrieve an instance of the ProductMetadataInterface. This interface provides methods for retrieving environment information.

The getEnvironment() method returns a multi-dimensional array containing various environment-related data. The structure is similar to what you see in the env.php file.

Note that directly accessing the ObjectManager like this is generally discouraged, as it can lead to memory leaks and other issues. It’s better to use dependency injection whenever possible within your Magento modules or plugins.

Conclusion

Accessing env.php variables from custom PHP code in Magento 2 requires some careful handling of the environment setup. By including the bootstrap file and using Magento’s ObjectManager, you can safely access these variables without having to manually load the env.php file yourself.

Remember to always use this approach responsibly and within the context of a valid Magento installation. Direct access to the ObjectManager should be avoided in production code where possible.

For more information on Magento 2 development best practices, I recommend checking out our comprehensive guide at Laravel Company. This guide covers everything from setup and configuration to advanced development techniques.

Magento 2 Catalog Admin Configuration

Our network