How To

How to Check If Magento 2 is Running In Production Mode

How are you all doing? Welcome all in the new article of How-To series and today we will learn How to check if Magento 2 is running in production mode. Previously, I have illustrated on How to Add Custom Block in Cart Summary Before Subtotal in Magento 2? with appropriate codes required to perform.

Introduction

Basically, the Magento 2 production mode is the most effective mode for all the Magento 2 websites in result the website is fully optimized and it operates smoothly and boosts the best customer experience. Mainly the use of production mode is very safe and secure with reason to it is if any inappropriate user tries to access the pub/static folder and actively performs changes to it then that will just make changes in the duplicate versions and that files will be overwritten during the next static deploy.

Steps of Coding

There are 2 ways to check if Magento 2 is running in production mode.

Method 1:  (Not Recommended Method)

$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @return \Magento\Framework\App\State */$state = $om->get('Magento\Framework\App\State');
/** @var bool $isDeveloperMode */$isDeveloperMode = \Magento\Framework\App\State::MODE_DEVELOPER === $state->getMode();

Note:  According to Magento Standard, Using Object Manager is not Recommended, We shared the way just for the knowledge purpose.

Method 2: Magento coding standard wise

protected $_appState;
public function __construct( \Magento\Framework\App\State $appState )
{
    $this->_appState = $appState;
}
public function doSomething() {
    switch ( $this->_appState->getMode() ) {
        case \Magento\Framework\App\State::MODE_DEFAULT:
            // Action for default mode
            break;
        case \Magento\Framework\App\State::MODE_PRODUCTION:
            // Action for production mode
            break;
        case \Magento\Framework\App\State::MODE_DEVELOPER:
            // Action for developer mode
            break;
    }
}

You can go for any method given above and implement for checking if Magento 2 is running in production mode. After the implementation, you will be successfully able to know the mode of your Magento 2 Store.

Conclusion

At last but not least if you find the article helpful do comment down your valuable reviews in the comment section below and for any queries, you get in implementing the above article then kindly contact our Support Team. Also, comment down any problem you face in Magento 2. We will create the best solution article for you and don’t forget to share with your Magento friends and help them in solving the queries. 

Happy Coding! 

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago