The tricky part of this action in a ZF application is that, apparently, your service will affect the application itself. Thus, if the application is โbrokenโ during maintenance, the risk is that the in-app solution may also break. In this sense, โexternalโ approaches, such as modifying .htaccess or setting up the public/index.php file, are probably more reliable.
However, the in-app approach may use the front controller plugin. In application/plugins/TimedMaintenance.php :
class Application_Plugin_TimedMaintenance extends Zend_Controller_Plugin_Abstract { protected $start; protected $end; public function __construct($start, $end) { // Validation to ensure date formats are correct is // left as an exercise for the reader. Ha! Always wanted // to do that. ;-) if ($start > $end){ throw new Exception('Start must precede end'); } $this->start = $start; $this->end = $end; } public function routeShutdown(Zend_Controller_Request_Abstract $request) { $now = date('Ymd H:i:s'); if ($this->start <= $now && $now <= $this->end){ $request->setModuleName('default') ->setControllerName('maintenance') ->setActionName('index'); } } }
Then register the plugin in application/Bootstrap.php :
protected function _initPlugin() { $this->bootstrap('frontController'); $front = $this->getResource('frontController'); $start = '2012-01-15 05:00:00'; $end = '2012-01-15 06:00:00'; $plugin = new Application_Plugin_TimedMaintenance($start, $end); $front->registerPlugin($plugin); }
In practice, you may need a start and end time before configuration. In application/configs/application.ini :
maintenance.enable = true maintenance.start = "2012-01-15 05:00:00" maintenance.end = "2012-01-15 06:00:00"
Then you can change the registration of the plugin to look like this:
protected function _initPlugin() { $this->bootstrap('frontController'); $front = $this->getResource('frontController'); $config = $this->config['maintenance']; if ($config['enable']){ $start = $config['start']; $end = $config['end']; $plugin = new Application_Plugin_TimedMaintenance($start, $end); $front->registerPlugin($plugin); } }
This way, you can enable maintenance mode by simply editing the configuration record.