Skip to content

Commit 252f90f

Browse files
authored
Merge pull request #7941 from cakephp/5.x-terminate-event
add docs for Server.terminate event
2 parents be95c0a + e3623ea commit 252f90f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

en/core-libraries/events.rst

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ application.
152152
* :ref:`Controller events <controller-life-cycle>`
153153
* :ref:`View events <view-events>`
154154

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+
155192
.. _registering-event-listeners:
156193

157194
Registering Listeners
@@ -204,7 +241,7 @@ of the ``EventListener`` interface. Internally, the event manager will use
204241
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::
205242

206243
namespace App;
207-
244+
208245
use App\Event\UserStatistic;
209246
use Cake\Event\EventManagerInterface;
210247
use Cake\Http\BaseApplication;

0 commit comments

Comments
 (0)