以编程方式获取Magento 2模式


Answers:


14

如果您指的是部署模式,则可以使用以下命令通过CLI进行操作:

php bin/magento deploy:mode:show

如果您需要通过纯代码执行此操作,则可以使用:

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;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.