How can i put condition of maintenance mode in index file or Bootstap file in magento 2 ?

I want to check if Maintenance mode i enable or disable conditional base.

How can i check that in Index.php file or Bootstrap.php file in Magento 2 ?


Try to use this code :


ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$instance = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$maintenanceMode = $instance ->get('\Magento\Framework\App\MaintenanceMode');
$flag = $maintenanceMode->isOn();

It maybe helpful for you.

  • Is it index.php file @rohan ? – Aditya Shah Sep 20 '18 at 5:56
  • Please check my answer with code. You can use it in index.php – Kishan Patadia Sep 20 '18 at 5:56
  • Yes. You can use in root file. – Rohan Hapani Sep 20 '18 at 5:56
  • Thanks man :) it's working file !! (3 mins to accept as answer :D) – Aditya Shah Sep 20 '18 at 6:00
  • 1
    Happy to help :) Happy Coding !! – Rohan Hapani Sep 20 '18 at 6:03

If var/.maintenance.flag file does not exist, Magento operates normally, and maintenance mode is off. If it exists, maintenance mode is on. So you can write code for the same.

$objManager = \Magento\Framework\App\ObjectManager::getInstance();
$maintenanceMode = $objManager->get('\Magento\Framework\App\MaintenanceMode');
$flag = $maintenanceMode->isOn();

Put below code in your index.php file

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

$maintenanceMode = $obj ->get('\Magento\Framework\App\MaintenanceMode');
$flag = $maintenanceMode->set(1);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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