Question

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!

Was it helpful?

Solution

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!

OTHER TIPS

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

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top