@@ -152,6 +152,43 @@ application.
152
152
* :ref: `Controller events <controller-life-cycle >`
153
153
* :ref: `View events <view-events >`
154
154
155
+ ``Server.terminate ``
156
+ --------------------
157
+
158
+ The ``Server.terminate `` event is triggered after the response has been sent to the
159
+ client. This event is useful for performing tasks that should be done after the
160
+ response has been sent, such as logging or sending emails.
161
+
162
+ You can listen to this event using an event manager instance::
163
+
164
+ use Cake\Event\EventManager;
165
+
166
+ EventManager::instance()->on('Server.terminate', function ($event) {
167
+ // Perform tasks that should be done after the response has been
168
+ // sent to the client.
169
+ });
170
+
171
+ Or using the ``events `` hook in your Application/Plugin class::
172
+
173
+ use Cake\Event\EventManagerInterface;
174
+
175
+ public function events(EventManagerInterface $eventManager): EventManagerInterface
176
+ {
177
+ $eventManager->on('Server.terminate', function ($event) {
178
+ // Perform tasks that should be done after the response has been
179
+ // sent to the client.
180
+ });
181
+
182
+ return $eventManager;
183
+ }
184
+
185
+ .. tip ::
186
+ This is called even if an exception is thrown during the request, e.g. on 404 pages.
187
+
188
+ .. note ::
189
+ The ``Server.terminate `` event only works for PHP-FPM implementations which
190
+ support the ``fastcgi_finish_request `` function.
191
+
155
192
.. _registering-event-listeners :
156
193
157
194
Registering Listeners
@@ -204,7 +241,7 @@ of the ``EventListener`` interface. Internally, the event manager will use
204
241
As of CakePHP 5.1 it is recommended to register event listeners by adding them via the ``events `` hook in your application or plugin class::
205
242
206
243
namespace App;
207
-
244
+
208
245
use App\Event\UserStatistic;
209
246
use Cake\Event\EventManagerInterface;
210
247
use Cake\Http\BaseApplication;
0 commit comments