CakePHP 2.1.0: How to Create a Down for Service Page

I am trying to implement something like Mark History "Down for Maintenance" with CakePHP 2.1.0. I'm pretty close to achieving this, but I have two problems that I could help with. First of all, here is all 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 problems that I am having. Firstly, this code only works when debugging is set above 1. Can I do something about this? Does this mean that I'm wrong? The second problem is that although I set the variables “isMobile” and “logged_in” for booleans in the “downForMaintenance” method, the file “app / View / Layouts / default.ctp” sees them as strings. What can i do with this?

Thanks!

+4
source share
3 answers

here is a quick and dirty service 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 rent your site and it will be available for viewing from your home / office.

BONUS: Works with all versions of the cake!

+16
source

A more elegant way would be to add a route that overlaps any other at the 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
source

We need to create the custom submit filter that you used for CakePHP. check below link

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

0
source

Source: https://habr.com/ru/post/1401058/


All Articles