zend-modules / maintenance
Maintenance mode component for ZF2
Requires
- php: >=5.3.23
- zendframework/zend-eventmanager: 2.4.8
- zendframework/zend-http: 2.4.8
- zendframework/zend-mvc: 2.4.8
- zendframework/zend-servicemanager: 2.4.8
- zendframework/zend-view: 2.4.8
This package is not auto-updated.
Last update: 2024-11-09 19:45:24 UTC
README
Installation
Main Setup
With composer (Recomended)
-
Add this project in your composer.json:
"require": { "zend-modules/maintenance": "dev-master" }
-
Now tell composer to download the maintenance mode component by running the command:
$ php composer.phar update
By cloning the project
- Clone this project into your
./vendor/
directory.
Warning This installation type will only allow the component to be installed as a module.
Post installation
There are two ways to set this component.
As a component
-
Enabling it in your
application.config.php
file.<?php return array( // ... 'service_manager' => array( 'factories' => array( 'MaintenanceConfig' => 'Maintenance\Service\MaintenanceConfigFactory', ), 'invokables' => array( 'MaintenanceListener => 'Maintenance\Service\MaintenanceListener', ), ), 'listeners' => array( 'MaintenanceListener', ), );
As a module
-
Enabling it in your
application.config.php
file.<?php return array( 'modules' => array( // ... 'Maintenance', ), // ... );
Configuration
The configuration must be made in your application.config.php
file. This is so as the maintenance module will take effect over all your application. The main entry will be maintenance_mode
.
Enable Maintenance Mode
-
Enabling it in your
application.config.php
file.<?php return array( // ... 'maintenance_mode' => array( 'enabled' => true, ), // ... );
The default value for enabled
is false
. Therefore, to disable you may comment out the line or set it to false
.
Enabled Maintenance Mode Access
You may enable certain IP addresses to access yur site during maintenance mode. To do so you must define the whitelist of IP addresses.
-
Set the whitelist in your
application.config.php
file.<?php return array( // ... 'maintenance_mode' => array( 'enabled' => true, 'whitelist' => array( '127.0.0.1', ), ), // ... );
Setting a custom template
-
Set the template path in your
application.config.php
file.<?php return array( // ... 'maintenance_mode' => array( 'enabled' => true, 'template' => dirname(__DIR__) . '/views/layout/maintenance.phtml', ), // ... );
Retry-After Header
The Retry-After
HTTP header may be sent for responses with a status code set to 503. If the config value is not set, set to a past date, or the status code is not 503; then the header will not be set.
-
Set the full date and time for the retry-after header in your
application.config.php
file.<?php return array( // ... 'retry_after' => '2015-12-07 00:00:00', // ... );
HTTP Status Code
By default, the server will return a 503 (Service Unavailable) HTTP status code when in maintenance mode. If you wish to change the HTTP status code for any reason you may do so.
-
Set the desired HTTP status code in your
application.config.php
file.<?php return array( // ... 'maintenance_mode' => array( 'status_code' => 500, ), // ... );
Runtime Configuration
Sometimes we may wish to set the maintenance mode options from another source such as a database backend. This can be done with no problem as the maintenance mode configuration is stored in the service manager. Simply make your changes on the bootstrap event. As an example:
```php
<?php
namespace Application;
use Zend\EventManager\EventInterface;
class Module
{
public function onBootstrap(EventInterface $e)
{
if (!$e instanceof MvcEvent) {
return;
}
$serviceManager = $e->getApplication()->getServiceManager();
$maintenanceConfig = $serviceManager->get('MaintenanceConfig');
$maintenanceConfig->setEnabled(true);
}
}
```