CakePHP 2.1.0:如何创建“向下维护”页面

时间:2012-03-12 20:57:03

标签: cakephp cakephp-2.1

我正在尝试使用CakePHP 2.1.0实现Mark Story's "Down for Maintenance" page之类的东西。我已经非常接近实现这一点,但我遇到了两个问题,我可以使用一些帮助。首先,这里是所有相关代码(六个文件):

1)app / Config / bootstrap.php:

Configure::write('App.maintenance', true);

2)app / Config / core.php:

Configure::write('debug', 1);

...

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException',
    'renderer' => 'AppExceptionRenderer',
    'log' => true
));

3)app / Controller / AppController.php:

if (Configure::read('App.maintenance') == true) {
    App::uses('DownForMaintenanceException', 'Error/Exception');
    throw new DownForMaintenanceException(null);
}

4)app / Lib / Error / Exception / DownForMaintenanceException.php:

<?php
class DownForMaintenanceException extends CakeException {}

5)app / Lib / Error / AppExceptionRenderer.php:

<?php
App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {
    function _outputMessage($template) {
        // Call the "beforeFilter" method so that the "Page Not Found" page will
        // know if the user is logged in or not and, therefore, show the links that
        // it is supposed to show.

        if (Configure::read('App.maintenance') == false)
        {
            $this->controller->beforeFilter();
        }

        parent::_outputMessage($template);
    }

    public function downForMaintenance() {
        $url = $this->controller->request->here();
        $code = 403;
        $this->controller->response->statusCode($code);
        $this->controller->set(array(
            'code' => $code,
            'url' => h($url),
            'isMobile' => $this->controller->RequestHandler->isMobile(),
            'logged_in' => false,
            'title_for_layout' => 'Down for Maintenance'
        ));
        $this->_outputMessage($this->template);
    }
}

6)app / View / Errors / down_for_maintenance.ctp:

<p>Down for Maintenance</p>

现在,对于我遇到的两个问题。首先,此代码仅在调试设置为高于1时才有效。我能做些什么吗?这是否表明我以错误的方式解决这个问题?第二个问题是,虽然我在“downForMaintenance”方法中将“isMobile”和“logged_in”视图变量设置为 boolean 值,但“app / View / Layouts / default.ctp”文件将它们视为字符串。我该怎么办?

谢谢!

3 个答案:

答案 0 :(得分:16)

这是cakephp的快速脏点维护页面

在公共index.php

define('MAINTENANCE', 0); 
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE')
{
require('maintenance.php'); die(); 
}

然后,当您想要关闭网站时,只需更改MAINTENANCE = 1即可从家中/办公室查看。

奖励:适用于所有版本的蛋糕!

答案 1 :(得分:3)

更优雅的方法是在routes.php的最顶部添加覆盖任何其他路线的路线:

//Uncomment to set the site to "under construction"
Router::connect('/*', array('controller' => 'pages', 'action' => 'underConstruction'));

//any other route should be underneath 

如果您想添加任何条件,您也可以在此处执行此操作:

define('MAINTENANCE', 0); 
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE')
    Router::connect('/*', array('controller' => 'pages', 'action' => 'underConstruction'));
}

答案 2 :(得分:0)

我们需要创建一个自定义的Dispatch Filter,CakePHP已经涵盖了你。  检查以下链接

http://josediazgonzalez.com/2013/12/13/simple-application-maintenance-mode/