Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable website for specific time in zend framework?

I am using zend framework. Sometimes I want to disable or down my site for specific time for maintenance. So how can I stop people to access any page of my site when needed ?

Where is the bottleneck in Zend Framework where I can stop all requests and stop user to proceed.

Thanks

like image 782
Student Avatar asked Dec 31 '11 09:12

Student


1 Answers

The tricky part of doing this within the ZF app is that presumably your maintenance will affect the app itself. So, if the app is "broken" during the maintenance, the risk is that an "in-app" solution may break, too. In that sense, "external" approaches like modifying .htaccess or tweaking the public/index.php file are probably more robust.

However, an "in-app" approach could utilize a 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('Y-m-d 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 might want to push the start/end times up to config. 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 could modify the plugin registration to look like:

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 simply by editing the config entry.

like image 112
David Weinraub Avatar answered Oct 25 '22 12:10

David Weinraub