gpt4 book ai didi

php - 如何在 zend 框架中的特定时间禁用网站?

转载 作者:可可西里 更新时间:2023-11-01 13:06:02 25 4
gpt4 key购买 nike

我正在使用 zend 框架。有时我想在特定时间禁用或关闭我的站点以进行维护。那么我怎样才能阻止人们在需要时访问我网站的任何页面呢?

Zend Framework 的瓶颈在哪里我可以停止所有请求并阻止用户继续。

谢谢

最佳答案

在 ZF 应用程序中执行此操作的棘手部分是,您的维护可能会影响应用程序本身。因此,如果应用程序在维护期间“损坏”,则存在“应用程序内”解决方案也可能损坏的风险。从这个意义上说,修改 .htaccess 或调整 public/index.php 文件等“外部”方法可能更可靠。

但是,“应用内”方法可以利用前端 Controller 插件。在 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);
}
}

这样,您只需编辑配置条目即可启用维护模式。

关于php - 如何在 zend 框架中的特定时间禁用网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8687392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com