12 Jul/10

An “Under Maintenance” plugin for your web application

Let’s make a bet: if you don’t have to maintain your (professional) web application within a few weeks after release, I owe you a pint of beer. Maintaining websites can be a complex tast: you may have to adopt your database schema, batch jobs, PHP files, and so on. As your web site will be down during these tasks, you should inform your users that your application is under maintenance and when it will be online again. This simple Zend Framework plugin might help you.


Basically, this plugin does the following: it checks whether or not your web site is under maintenance. If so, the user will be redirected to a “under maintenance” site. The admin however, will still be able to use the website.

Step 1: The plugin

Create a plugin called maintenance.php with the following lines of code:

<?php
/**
 * Maintenance Plugin
 *
 * @package    web-punk.com
 * @author     Christian Koncilia
 */

class Plugin_Maintenance extends Zend_Controller_Plugin_Abstract
{
   /**
   * Called before dispatcher starts
   *
   * @param  Zend_Controller_Request_Abstract $request
   * @return void
   */
   public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
   {
      // is user = admin?
      if (isset($_SESSION['heistheadmin'])) {
         return $request;
      }

      // knock, knock - it's me: your admin!
      $alnumfilter = new Zend_Filter_Alnum();
      $adminparam = $alnumfilter ->filter($this->getRequest()->getParam ( 'iamtheadmin' ));
      // change "password" to a secure password that the admin may use
      // e.g. something like 8kL27XePPa
      if ("password" == $adminparam) {
         // if user = admin, make sure that he no longer has to provide a password
         $_SESSION['heistheadmin'] = 'true';
         return $request;
      }

      // if user is not admin, redirect to maintenance site
      $request->setControllerName('index')
              ->setActionName('maintenance')
              ->setDispatched(false);
   }
}

Before using this plugin make sure to set a secure password (line 29 in the source code)!

As you can see in the source code, the plugin redirects the user to the maintenance action in the index controller. So, this is where you might want to add additional logic. After creating this action in your index controller, simply create a maintenance.phtml file in the folder containing the views (e.g. \application\views\scripts\index). This could be as simple as:

<h1>We are currently under maintenance! Please, come back again in a few minutes</h1>

Step 2: Register your plugin

Basically, thats all you have to do. Now, in case that you have to maintain your website, all you have to do is to load your maintenance plugin in your bootstrap, e.g.:

// list of plugins to load
$pluginList = array(
 'plugin1'     => $loader->load('plugin1'),
 'plugin2'     => $loader->load('plugin2'),
 'maintenance' => $loader->load('Maintenance'),
);

// register plugins
foreach ($pluginList as $pluginClass) {
  $frontController->registerPlugin(new $pluginClass());
}

If you don’t want to edit your bootstrap each time your website is under maintenance, simply insert a maintenance parameter in your config.ini and use something like this in your bootstrap to register your plugins:

// list of plugins to load
$pluginList = array(
 'plugin1'     => $loader->load('plugin1'),
 'plugin2'     => $loader->load('plugin2'),
);

// if maintenance mode is on, add maintenance plugin
if ($config->maintenance != 0) {
	$pluginList['maintenance'] = $loader->load('Maintenance');
}

// register plugins
foreach ($pluginList as $pluginClass) {
  $frontController->registerPlugin(new $pluginClass());
}

Posted in Tutorial and Zend Framework by Christian on July 12th, 2010 at 9:22 AM.

3 comments - 4 pingbacks / trackbacks

3 Replies

  1. IchBinIch Jul 12th 2010

    A very nice post!

    Thanks, for the “many little tricks & hints” about Zend! :)

  2. You are welcome! ;-)

  3. Horoskop Sep 10th 2010

    Thanks


Leave a Reply


Site tools