How to Check If Magento 2 is Running In Production Mode

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! 

Previous Article

How to Redirect Customers to The Previous Page After Login in Magento 2.

Next Article

Top 10 Magento 2 Hosting Providers (Updated 2024)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨