1

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.

2

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

0

If you find var/.maintenance.flag file then your Magento 2 store is in maintenance mode. It is hidden file so you may need to turn on show hidden files.

  • so i need to use the file_exists but i'm quite new in Magento is that possible to made a function in my module which go search out of the module ? – Antoine553 Jul 13 '17 at 12:57
0

Magento detects maintenance mode as follows:

  1. If var/.maintenance.flag does not exist, maintenance mode is off and Magento operates normally.

  2. Otherwise, maintenance mode is on unless var/.maintenance.ip exists

Command usage:

magento maintenance:enable [--ip=<ip address> ... --ip=<ip address>] | [ip=none]
magento maintenance:disable [--ip=<ip address> ... --ip=<ip address>] | [ip=none]
magento maintenance:status
0

When Maintenance Mode is enabled Magento creates a flag file in var/.maintenance.flag

Check this file

vendor/magento/framework/App/MaintenanceMode.php

function

/**
 * Sets maintenance mode "on" or "off"
 *
 * @param bool $isOn
 * @return bool
 */
public function set($isOn)
{
    if ($isOn) {
        return $this->flagDir->touch(self::FLAG_FILENAME);
    }
    if ($this->flagDir->isExist(self::FLAG_FILENAME)) {
        return $this->flagDir->delete(self::FLAG_FILENAME);
    }
    return true;
}

To Check if maintenance mode is enabled check this function

/**
 * Checks whether mode is on
 *
 * Optionally specify an IP-address to compare against the white list
 *
 * @param string $remoteAddr
 * @return bool
 */
public function isOn($remoteAddr = '')
{
    if (!$this->flagDir->isExist(self::FLAG_FILENAME)) {
        return false;
    }
    $info = $this->getAddressInfo();
    return !in_array($remoteAddr, $info);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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