In Drupal 8, the Site Maintenance status is stored in State API.
To put Site in Maintenance mode or to get Site out of Maintenance mode, we can use following code snippet:
<?php
\Drupal::state()->set('system.maintenance_mode', 1);
// Clear render cache to make sure user are not able to access site.
Cache::invalidateTags(['rendered']);
?>
The value \Drupal::state()->set('system.maintenance_mode', 1);
// Clear render cache to make sure user are not able to access site.
Cache::invalidateTags(['rendered']);
?>
1
specify maintenance mode is enabled and 0
specify maintenance mode is disabled.