14

What's the best way to get the current mode Magento 2 is running with code?

In my case it's for a headless Magento, but I can see a number of cases where this might be required.

1 Answer 1

22

If you're referring to the deployment mode, you can do it via the CLI using:

php bin/magento deploy:mode:show

If you need to do it via pure code you can use:

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;
    }
}
1
  • I am getting type error Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Pmmsite\PmmLayout\Block\Head\Interceptor Commented May 13, 2020 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.