我正在使用zend框架。有时我想在特定的维护时间内禁用或关闭我的网站。那么如何在需要时阻止人们访问我网站的任何页面?
Zend Framework中的瓶颈在哪里我可以停止所有请求并阻止用户继续。
由于
答案 0 :(得分:7)
很简单。
使用htaccess
重写并在静态临时页面上重定向所有请求,而不是将其发送到bootstrap
或index
文件
答案 1 :(得分:7)
.htaccess文件中的重写规则通过index.php路由所有流量,因此如果你不能改变.htaccess,只需在你的index.php中放入以下行,然后再与任何ZF相关的东西。
$maintenanceStart = new DateTime('2012-01-01 00:00:00');
$maintenanceEnd = new DateTime('2012-01-01 01:00:00');
$now = new DateTime;
if ($now > $maintenanceStart && $now < $maintenanceEnd) {
fpassthru('/path/to/your/maintenancePage.html');
exit;
}
这样,在维护窗口期间不会执行任何与ZF相关的代码。
答案 2 :(得分:7)
在 ZF应用程序中执行此操作的棘手部分是,您的维护可能会影响应用程序本身。因此,如果应用程序在维护期间“损坏”,那么“应用内”解决方案的风险也可能会中断。从这个意义上说,修改.htaccess或调整public/index.php
文件等“外部”方法可能更强大。
然而,“应用程序内”方法可以使用前端控制器插件。在application/plugins/TimedMaintenance.php
:
class Application_Plugin_TimedMaintenance extends Zend_Controller_Plugin_Abstract
{
protected $start;
protected $end;
public function __construct($start, $end)
{
// Validation to ensure date formats are correct is
// left as an exercise for the reader. Ha! Always wanted
// to do that. ;-)
if ($start > $end){
throw new Exception('Start must precede end');
}
$this->start = $start;
$this->end = $end;
}
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$now = date('Y-m-d H:i:s');
if ($this->start <= $now && $now <= $this->end){
$request->setModuleName('default')
->setControllerName('maintenance')
->setActionName('index');
}
}
}
然后在application/Bootstrap.php
注册插件:
protected function _initPlugin()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$start = '2012-01-15 05:00:00';
$end = '2012-01-15 06:00:00';
$plugin = new Application_Plugin_TimedMaintenance($start, $end);
$front->registerPlugin($plugin);
}
实际上,您可能希望将开始/结束时间推送到配置。在application/configs/application.ini
:
maintenance.enable = true
maintenance.start = "2012-01-15 05:00:00"
maintenance.end = "2012-01-15 06:00:00"
然后您可以将插件注册修改为:
protected function _initPlugin()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$config = $this->config['maintenance'];
if ($config['enable']){
$start = $config['start'];
$end = $config['end'];
$plugin = new Application_Plugin_TimedMaintenance($start, $end);
$front->registerPlugin($plugin);
}
}
这样,您只需编辑配置条目即可启用维护模式。