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.
4 Answers
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
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 ? Jul 13, 2017 at 12:57
Magento detects maintenance mode as follows:
If var/.maintenance.flag does not exist, maintenance mode is off and Magento operates normally.
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
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);
}