Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

How to get value from env.php in Magento 2?

Hello Devs, In today’s blog, we are going to see how we can get value from env.php in Magento 2.

How to get value from env.php in Magento 2?
Written by
JKJigar Karangiya
Published
Reading time
1 min read

Jigar Karangiya · · 1 min read

Hello Devs, In today’s blog, we are going to see how we can get value from env.php in Magento 2.

As Magento 2 developers, we all are familiar with the env.php file, and it has to have an important role in Magento as it is the place where Magento 2 stores the database credentials, backend URL, enabled cache types, etc. The app/etc/env.php (opens in new tab) file is autogenerated. It stores a piece of information which is unique to your Magento 2 installation.

Ever wondered, how we can access those values in our code programmatically?

During development in some cases, you will need to access those values in your custom code. You can easily retrieve values from this file using the use of \Magento\Framework\App\DeploymentConfig class.

Let’s see a quick example demonstrating how to access a “database name ” of your Magento installation.

php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 
declare(strict_types=1);
 
namespace VendorName\ModuleName\Helper;
 
class Data {
 
    private const DB_NAME_PATH = 'db/connection/default/dbname';
    
    /**
     * @var \Magento\Framework\App\DeploymentConfig
     */
    protected $deploymentConfig;
 
    /**
     * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
     */
    public function __construct(
        \Magento\Framework\App\DeploymentConfig $deploymentConfig
    ) {
        $this->deploymentConfig = $deploymentConfig;
    }
 
    public function getDatabaseName()
    {   
        $databaseName = $this->deploymentConfig->get(self::DB_NAME_PATH);
        
        return $databaseName;
 
    }
}

In the above example when you call this function it will return the database name.

You can change this code as per your requirement.

You may also like :

How to create a root script in Magento 2?

How to configure Magento 2 coding standards in visual studio code?

Get Serialize value in array format using Magento 2

That’s it for this tutorial, See you in the next blog. Thank You !!

Keep reading
All posts

01 · 10 min · Shares Adobe Commerce Core

Adobe Commerce Agentic Capabilities, Q4 2026

Five agentic releases for Adobe Commerce: LLM product discovery, Brand Visibility, the Shopper MCP server, Brand Concierge, and Experience Workspace. What each one does, which editions get it, and when.

10 min

03 · 3 min · Shares Adobe Commerce Core

Conditions for Collections in Magento 2

Collections in Magento 2 (Now Adobe Commerce), are among the most powerful tools for retrieving and manipulating data directly from the database.

3 min

Newsletter

New Magento tutorials, straight to your inbox

Code examples, performance tips, and fixes for common Magento issues. No spam.

Subscribe