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.

5 Answers 5

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
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.

1
  • 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
    Commented Jul 13, 2017 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);
}
0

add the code in pub/index.php maintence set for all ips expect in given array ips

<?php
/**
 * Public alias for the application entry point
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Framework\App\Bootstrap;

try {
    require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else {
    $ip_address = $_SERVER['REMOTE_ADDR'];
}
$ip_list = ["102.103.104.105", "102.111.112.34"];

//$ip_list = [];

$show_page = false;
foreach ($ip_list as $ip) {
    if (strpos($ip_address, $ip) !== false) {
        $show_page = true;
        break;
    }
}
if ($show_page == false) {
    require_once 'errors/processorFactory.php';
    $processorFactory = new \Magento\Framework\Error\ProcessorFactory();
    $processor = $processorFactory->createProcessor();
    $response = $processor->process503();
    $response->sendResponse();
    die();
}

$bootstrap = Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

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.