我正在尝试使用CakePHP 2.1.0实现类似Mark Story的"Down for Maintenance"页面的功能。我很快就要做到这一点了,但我遇到了两个问题,我需要一些帮助。首先,这里是所有相关的代码(六个文件):
1) app/Config/bootstrap.hp:
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_emaintenance.ctp:
<p>Down for Maintenance</p>
现在,针对我正在经历的两个问题。首先,只有当debug设置为高于1时,此代码才能工作。对此我能做些什么吗?这是否表明我做这件事的方式不对?第二个问题是,尽管我在"downForMaintenance"方法中将"isMobile"one_answers"logged_in"视图变量设置为布尔值,但"app/view/Layouts/default.ctp"文件将它们视为string。我该怎么办?
谢谢!
这里有一个cakefp 的快速而肮脏的维护页面
在公共index.php
define('MAINTENANCE', 0);
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE')
{
require('maintenance.php'); die();
}
然后,当你想关闭你的网站时,只需更改"维护"=1,它仍然可以从你的家/办公室查看。
奖金:适用于所有版本的蛋糕!
一种更优雅的方法是在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'));
}
我们需要创建一个自定义的Dispatch Filter,CakePHP已经为您介绍过了。检查以下链接
http://josediazgonzalez.com/2013/12/13/simple-application-maintenance-mode/