Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.1.0: How to Create "Down for Maintenance" Page

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!

like image 904
Nick Avatar asked Dec 01 '22 00:12

Nick


1 Answers

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!

like image 148
jbrass Avatar answered Dec 04 '22 10:12

jbrass