approach for “site down for maintenance”

假装没事ソ 提交于 2019-12-05 10:26:26

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 :)

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.

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).

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

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!