CakePHP 2.1.0: How to Create “Down for Maintenance” Page

我与影子孤独终老i 提交于 2019-12-04 13:25:46

问题


I'm trying to implement something like Mark Story's "Down for Maintenance" page using CakePHP 2.1.0. I'm pretty close to achieving this, but I'm running into two issues that I could use some help with. First of all, here is all of the relevant code (six files):

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>

Now, for the two issues I'm experiencing. First, this code only works when debug is set higher than 1. Is there anything I can do about that? Does that indicate that I'm going about this the wrong way? The second issue is that, although I'm setting the "isMobile" and "logged_in" view variables to boolean values in the "downForMaintenance" method, the "app/View/Layouts/default.ctp" file is seeing them as strings. What can I do about that?

Thanks!


回答1:


here is a quick and dirty maintenance page for cakephp

in public index.php

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

Then just change MAINTENANCE = 1 when you want to take your site down and it will still be viewable from your home/office.

BONUS: Works with all versions of cake!




回答2:


A more elegant way would be to add a route overriding any other one at the very top of routes.php:

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

//any other route should be underneath 

If you want to add any condition you can also do it here:

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



回答3:


We'll need to create a custom Dispatch Filter,CakePHP has you covered. check below link

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



来源:https://stackoverflow.com/questions/9674696/cakephp-2-1-0-how-to-create-down-for-maintenance-page

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