From 29bf13f13dceb193ecac0e7725c6e38a83b1bf4e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 1 Jan 2024 22:12:15 -0500 Subject: [PATCH 1/2] Add docs for Server.terminate event Refs cakephp/cakephp#17514 --- en/appendices/5-1-upgrade-guide.rst | 4 ++++ en/controllers/request-response.rst | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) 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..18217dbb90 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,26 @@ 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:: + + $server = new Server(new Application(dirname(__DIR__) . '/config')); + + $request = ServerRequest::fromGlobals(); + $response = $server->run($request); + $server->emit($response); + $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 ======================================== From 206590155593a3a03a4bec971803e8be758f2798 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 12 Jan 2024 16:04:40 -0500 Subject: [PATCH 2/2] Improve example --- en/controllers/request-response.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/en/controllers/request-response.rst b/en/controllers/request-response.rst index 18217dbb90..af4866dde0 100644 --- a/en/controllers/request-response.rst +++ b/en/controllers/request-response.rst @@ -1074,11 +1074,12 @@ 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:: - $server = new Server(new Application(dirname(__DIR__) . '/config')); + $app = new Application(dirname(__DIR__) . '/config')) + $server = new Server($app); - $request = ServerRequest::fromGlobals(); - $response = $server->run($request); + $response = $server->run(ServerRequest::fromGlobals()); $server->emit($response); + $request = $app->getContainer()->get(ServerRequest::class); $server->terminate($request, $response); .. warning::