“现场维修"的方法. [英] approach for "site down for maintenance"

查看:98
本文介绍了“现场维修"的方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Joomla,我喜欢它的管理功能,可以将网站放下进行维护.如我所见,如果网站处于维护模式,那么对该网站的所有请求都将路由到单个页面.如果我想为非Joomla网站添加自己的维护站点"模块,该怎么做?我在PHP 2中使用了一个称为Kohana的MVC框架,其版本2与Codeigniter相似.我有一个Router类,可以在其中控制某个地址的位置.我能想到的唯一方法是在站点关闭时将每个请求重定向到特定的控制器功能,但是我该怎么做?我可能无法手动重新路由所有网址,对吗?

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 : 您可以在Kohana::modules()行之前在bootstrap.php中定义一个包罗万象的路由:

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 : 您将需要扩展Controller并处理构造函数中的维护中"页面显示(但您需要确保所有控制器都扩展该控制器类而不是普通的控制器类):

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;
        }
    }
}

或者甚至可以通过在hooks文件夹中添加文件(请确保在config.php中启用了钩子)来利用钩子系统来做到这一点:

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'));
    }
}

如您所见,在Kohana中实际上有很多方法可以做事,因为它是一个非常灵活的PHP框架:)

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

这篇关于“现场维修"的方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆