approach for "site down for maintenance"
Asked Answered
V

7

5

I have been using Joomla and I love its administrative facility to put the site down for maintenance. As I have seen, all requests to the site if it is in maintenance mode is routed to a single page. If I want to add my own "site down for maintenance" module for a non-Joomla site, how do I do this? I am using an MVC framework in PHP called Kohana in its version 2 which is similar with Codeigniter. I have a Router class where I can control where a certain address go. The only approach I can think is redirect every request to a particular controller function when site is down, but how do I do this? I can't possibly manually re-route all urls right?

Volk answered 3/12, 2009 at 6:20 Comment(0)
S
6

Kohana 3: You can define a catch-all route in your bootstrap.php before the Kohana::modules() lines:

if (/* check if site is in under maintenance mode */) {
    Route::set('defaulta', '(<id>)', array('id' => '.*'))
        ->defaults(array(
            'controller' => 'errors',
            'action'     => 'maintenance',
        ));
}

Or you can even mess with the request to do the same:

if (/* check if site is in under maintenance mode */) {
    echo Request::factory('errors/maintenance')
        ->execute()
        ->send_headers()
        ->response;
}

Kohana 2: You would need to extend Controller and handle the 'under maintenance' page display in the constructor (but you need to make sure all your controllers extend this controller class instead of the vanilla one):

abstract class Custom_Controller extends Controller {

    public function __construct()
    {
        parent::__construct();
        if (/* check if site is in under maintenance mode */) {
            $page = new View('maintenance');
            $page->render(TRUE);
            exit;
        }
    }
}

Or you can even utilize the hook system to do it, by adding a file in your hooks folder (make sure you enable hooks in your config.php):

Event::add('system.ready', 'check_maintenance_mode');

function check_maintenance_mode() {
    if (/* check if site is in under maintenance mode */) {
        Kohana::config_set('routes', array('_default' => 'errors/maintenance'));
    }
}

As you can see, there are actually many ways how to do stuff in Kohana because it's a very flexible PHP framework :)

Shrieve answered 4/12, 2009 at 3:9 Comment(0)
Z
9

Take a look at the routing documentation. You should be able to use a regular expression that redirects any uri to a specific controller/action. The only question left would be how to turn that rule on/off.

Zollverein answered 3/12, 2009 at 6:32 Comment(0)
S
6

Kohana 3: You can define a catch-all route in your bootstrap.php before the Kohana::modules() lines:

if (/* check if site is in under maintenance mode */) {
    Route::set('defaulta', '(<id>)', array('id' => '.*'))
        ->defaults(array(
            'controller' => 'errors',
            'action'     => 'maintenance',
        ));
}

Or you can even mess with the request to do the same:

if (/* check if site is in under maintenance mode */) {
    echo Request::factory('errors/maintenance')
        ->execute()
        ->send_headers()
        ->response;
}

Kohana 2: You would need to extend Controller and handle the 'under maintenance' page display in the constructor (but you need to make sure all your controllers extend this controller class instead of the vanilla one):

abstract class Custom_Controller extends Controller {

    public function __construct()
    {
        parent::__construct();
        if (/* check if site is in under maintenance mode */) {
            $page = new View('maintenance');
            $page->render(TRUE);
            exit;
        }
    }
}

Or you can even utilize the hook system to do it, by adding a file in your hooks folder (make sure you enable hooks in your config.php):

Event::add('system.ready', 'check_maintenance_mode');

function check_maintenance_mode() {
    if (/* check if site is in under maintenance mode */) {
        Kohana::config_set('routes', array('_default' => 'errors/maintenance'));
    }
}

As you can see, there are actually many ways how to do stuff in Kohana because it's a very flexible PHP framework :)

Shrieve answered 4/12, 2009 at 3:9 Comment(0)
K
2

You could check a database flag in a common header (presumably you've got some common include which connects to the database etc), and if the flag is set, render a particular page and exit (making sure to do all the usual cleanup things like closing database connections etc).

Khalkha answered 3/12, 2009 at 6:25 Comment(0)
D
1

Are you running Apache? Add this to your .htaccess file in your web root (or to the vhost declaration for the site) to redirect ALL requests to a "under maintenance" page:

Redirect 301 / /maintenace_page.html

Diatomic answered 3/12, 2009 at 8:1 Comment(2)
that would be good but can I turn that off with a php script if I want to?Volk
Sure. Just have a PHP script edit .htaccess and replace Redirect 301 / /maintenace_page.html with #Redirect 301 / /maintenace_page.html - the # denotes a comment line, and the redirect won't be executed.Diatomic
K
0

I can think of two ideas to make this happen.

  1. Like Larry said, use regular expressions to redirect all requests to a specific location. So, first you would determine if the site was in maintenance mode (probably using a database flag or a config file setting), then if it was, use the regular expression feature of routing to redirect all traffic to one place.

  2. You could check if the site was in maintenance mode in the constructor of every controller and redirect as needed. This is probably not the most optimal solution since you would be repeating the same code for each controller. So, even though it would work, you would probably be better off with the regular expressions.

Routing in Kohana: http://docs.kohanaphp.com/general/routing

PHP Regular expressions: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

Controllers in Kohana: http://docs.kohanaphp.com/general/controllers

Khaddar answered 3/12, 2009 at 6:38 Comment(0)
Q
0

In your index.php define a constant called IN_MAINTENANCE

Create a callback like so in a hook file:

function in_maintenance()
{
    if(IN_MAINTENANCE)
    {
         Router::$controller = 'my_maintenance_controller';
         Router::$method = 'index';
    }
}

And add it as into the system.post_routing event.

Event::add('system.post_routing', 'in_maintenance');

When you upload your site change the value of IN_MAINENANCE to TRUE and all requests will be redirected to your maintenance page.

See the Event / Hook documentation for more info on creating a hook.

Quinone answered 3/12, 2009 at 9:47 Comment(0)
U
0

Another method of toggling this is by checking for a file: if it exists, maintenance is starting so turn the site off.

If so, you can do any of the things mentioned above.

Unbeaten answered 3/12, 2009 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.