【发布时间】:2011-12-31 09:28:17
【问题描述】:
我正在使用 zend 框架。有时我想在特定的维护时间禁用或关闭我的站点。那么如何在需要时阻止人们访问我网站的任何页面?
Zend Framework 的瓶颈在哪里,我可以停止所有请求并阻止用户继续。
谢谢
【问题讨论】:
标签: php zend-framework
我正在使用 zend 框架。有时我想在特定的维护时间禁用或关闭我的站点。那么如何在需要时阻止人们访问我网站的任何页面?
Zend Framework 的瓶颈在哪里,我可以停止所有请求并阻止用户继续。
谢谢
【问题讨论】:
标签: php zend-framework
在 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);
}
}
这样,您只需编辑配置条目即可启用维护模式。
【讨论】:
很简单。
使用htaccess重写并重定向静态临时页面上的所有请求,而不是将它们发送到bootstrap或index文件
【讨论】:
.htaccess 对我来说总是很困难 :) zend 框架中到底有没有?
.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 相关的代码。
【讨论】: