English 中文(简体)
approach for "site down for maintenance"
原标题:
  • 时间:2009-12-03 06:20:52
  •  标签:
  • php
  • kohana

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?

最佳回答

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签