2012-03-12 68 views
4

我试图用CakePHP 2.1.0实现类似Mark Story's "Down for Maintenance" page的东西。我相当接近实现这一点,但我遇到了两个问题,我可以使用一些帮助。首先,这里是所有相关的代码(六个文件):CakePHP 2.1.0:如何创建“维护下来”页面

1)应用程序/配置/ bootstrap.php中:

Configure::write('App.maintenance', true); 

2)应用程序/配置/ core.php中:

Configure::write('debug', 1); 

... 

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException', 
    'renderer' => 'AppExceptionRenderer', 
    'log' => true 
)); 

3)应用程序/控制器/ AppController.php:

if (Configure::read('App.maintenance') == true) { 
    App::uses('DownForMaintenanceException', 'Error/Exception'); 
    throw new DownForMaintenanceException(null); 
} 

4)应用程序/ LIB /错误/异常/ DownForMaintenanceException.php:

<?php 
class DownForMaintenanceException extends CakeException {} 

5)应用程序/ LIB /错误/ 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)应用程序/视图/错误/ down_for_maintenance.ctp:现在

<p>Down for Maintenance</p> 

,对于两个我遇到的问题。首先,只有在调试设置为高于1时,此代码才有效。有什么我可以做的吗?这是否表明我正在以这种错误的方式去做?第二个问题是,尽管我在“downForMaintenance”方法中将“isMobile”和“logged_in”视图变量设置为布尔值值,但“app/View/Layouts/default.ctp”文件将其视为字符串。我能做些什么?

谢谢!

+0

您是否有第3部分写在类定义之前? – func0der 2012-11-16 20:58:18

+0

还有一个控制台的方式来打开/关闭这里描述:[移动a-cakephp-app](http://www.dereuromark.de/2013/09/29/moving-a-cakephp-app/ ) – mark 2014-07-15 10:46:38

回答

16

这里是公共的index.php为CakePHP的

一个快速和肮脏的维护页面

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

然后,只需更改维护当你想利用你的网站上下= 1,它仍然是可见的从你家/办公室。

奖励:适用于所有版本的蛋糕!

+2

我喜欢它!谢谢! – Nick 2012-03-12 21:58:46

+0

太棒了!谢谢! – 2012-04-26 11:00:57

3

更优雅的方式是添加覆盖在routes.php最高层的任何其他一个路线:

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

//any other route should be underneath 

如果你想添加你可以在任何条件在这里也可以这样做:

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