4

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!

Nick
  • 8,049
  • 18
  • 63
  • 107
  • Do you have part 3 written before the class definition itself? – func0der Nov 16 '12 at 20:58
  • There is also a console way to turn it on/off described here: [moving-a-cakephp-app](http://www.dereuromark.de/2013/09/29/moving-a-cakephp-app/) – mark Jul 15 '14 at 10:46

3 Answers3

16

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!

jbrass
  • 941
  • 7
  • 26
3

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'));
}
Alvaro
  • 40,778
  • 30
  • 164
  • 336
0

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/

apurav gaur
  • 342
  • 7
  • 18