ZF2维护页面 [英] ZF2 maintenance page

查看:66
本文介绍了ZF2维护页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 ZF2 设置维护页面,但它不起作用.我在公共文件夹(www)和我的 onbootstrap 函数中放置了一个 maintenance.html 页面,我有以下代码:

I try to set up a maintenance page with ZF2 but it's not working. I put a maintenance.html page in public folder (www) and in my onbootstrap function I've got the following code :

    $config = $e->getApplication()->getServiceManager()->get('Appli\Config'); 
    if($config['maintenance']) {
        $response  = $e->getResponse();
        $response->getHeaders()->addHeaderLine('Location', '/maintenance.html');
        $response->setStatusCode(503);
        return $response;
    }

我输入 if 原因 $config['maintenance'] 为 true 但它没有按预期显示我的 maintenance.html 页面.相反,它显示所询问的页面.我的重定向有问题吗?

I enter the if cause $config['maintenance'] is true but it's not displaying my maintenance.html page as expected. Instead it displays the page asked. Is there something wrong about my redirection ?

推荐答案

您似乎试图直接从您的 onBootstrap 方法短路请求.那是行不通的,那时路由还没有被解析,控制器也没有被调度.本质上,您所做的只是预先填充响应,只有在路由和分派请求后才会覆盖它.

You appear to be attempting to short-circuit the request directly from your onBootstrap method. That won't work, at that point the route hasn't been resolved and the controller hasn't been dispatched. Essentially, all you're doing is pre-populating the response, only for it to be over-written once the request is routed and dispatched.

如果您想影响响应,您需要监听其他 MvcEvent 之一.似乎您想在调度控制器之前执行此操作,因此执行此操作的位置将在 EVENT_ROUTE 中,理想情况下具有高优先级,因此它会在路由器解析路由之前发生(保存浪费处理解决永远不会被分派的路由).

If you want to affect the response, you'll need to listen to one of the other MvcEvents. It seems you want to do this before a controller is dispatched, so the place to do it would be in the EVENT_ROUTE, ideally with a high priority so it happens before the route is resolved by the router (saves wasted processing resolving a route that will never be dispatched).

public function onBootstrap(MvcEvent $e)
{
    $events = $e->getApplication()->getEventManager();
    $events->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $r) {
        $config = $r->getApplication()->getServiceManager()->get('Appli\Config');
        if ($config['maintenance']) {
            $response = $r->getResponse();
            // set content & status
            $response->setStatusCode(503);
            $response->setContent('<h1>Service Unavailable</h1>');
            // short-circuit request...
            return $response;
        }
    }, 1000);
}

这篇关于ZF2维护页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆