Magento 2.1 Maintenance – Detect Maintenance Mode in Magento 2

magento-2.1maintenance

I'm actually trying to find a function which has to detect if the website is in maintenance mode or not do someone have the answer please? I know we can do it in the Console but I need it in one of my bloc.

Best Answer

Let's say you want to detect this inside of one of your classes.
You need to add this dependency to it Magento\Framework\App\MaintenanceMode.

Make your class constructor look like this:

protected $maintenanceMode;
public function __construct(
    ....
    \Magento\Framework\App\MaintenanceMode $maintenanceMode,
    ....
) {
    ....
    $this->maintenanceMode = $maintenanceMode;
    ....
}

Then you can call inside of your class this:

$flag = $this->maintenanceMode->isOn();

you can even specify an ip address as parameter for isOn

Related Topic