Stu*_*ent 6 php zend-framework
我正在使用zend框架.有时我想在特定的维护时间内禁用或关闭我的网站.那么如何在需要时阻止人们访问我网站的任何页面?
Zend Framework中的瓶颈在哪里我可以停止所有请求并阻止用户继续操作.
谢谢
.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;
}
Run Code Online (Sandbox Code Playgroud)
这样,在维护窗口期间不会执行任何与ZF相关的代码.
这样做的最棘手的部分内的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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在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);
}
Run Code Online (Sandbox Code Playgroud)
实际上,您可能希望将开始/结束时间推送到配置.在application/configs/application.ini:
maintenance.enable = true
maintenance.start = "2012-01-15 05:00:00"
maintenance.end = "2012-01-15 06:00:00"
Run Code Online (Sandbox Code Playgroud)
然后你可以修改插件注册看起来像:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这样,您只需编辑配置条目即可启用维护模式.