diff --git a/en/appendices/5-1-upgrade-guide.rst b/en/appendices/5-1-upgrade-guide.rst index 34a8216798..1287cb0833 100644 --- a/en/appendices/5-1-upgrade-guide.rst +++ b/en/appendices/5-1-upgrade-guide.rst @@ -40,6 +40,10 @@ Http - ``Client`` now emits ``HttpClient.beforeSend`` and ``HttpClient.afterSend`` events when requests are sent. You can use these events to perform logging, caching or collect telemetry. +- ``Http\Server::terminate()`` was added. This method triggers the + ``Server.terminate`` event which can be used to run logic after the response + has been sent in fastcgi environments. In other environments the + ``Server.terminate`` event runs *before* the response has been sent. TestSuite --------- diff --git a/en/controllers/request-response.rst b/en/controllers/request-response.rst index 47f1c5671c..af4866dde0 100644 --- a/en/controllers/request-response.rst +++ b/en/controllers/request-response.rst @@ -1040,7 +1040,7 @@ will make the browser remove its local cookie:: .. _cors-headers: Setting Cross Origin Request Headers (CORS) -=========================================== +------------------------------------------- The ``cors()`` method is used to define `HTTP Access Control `__ @@ -1067,6 +1067,27 @@ criteria are met: is very application specific. We recommend you build your own ``CORSMiddleware`` if you need one and adjust the response object as desired. +Running logic after the Response has been sent +---------------------------------------------- + +In fastcgi based environments you can listen to the ``Server.terminate`` event +to run logic **after** the response has been sent to the client. Make sure your +application's **webroot/index.php** contains the following:: + + $app = new Application(dirname(__DIR__) . '/config')) + $server = new Server($app); + + $response = $server->run(ServerRequest::fromGlobals()); + $server->emit($response); + $request = $app->getContainer()->get(ServerRequest::class); + $server->terminate($request, $response); + +.. warning:: + In non fastcgi environments the ``Server.terminate`` event is fired before + the response is sent. + +.. versionadded:: 5.1.0 + Common Mistakes with Immutable Responses ========================================