Core\Task::runAfterResponse
Core\Task::runAfterResponse() runs a callable after the response is sent, so the visitor does not wait for it.
Use it for work the visitor does not need to see: a receipt email, a webhook, an audit log. The call only adds the callable to a list. The callables run when the request is finished, one at a time, in the order you added them. In a command-line program, they run when the script ends.
Good to know: the work is not saved anywhere. If the request ends with an uncaught error or exit, the callables do not run. If the process stops, the work is lost and nothing tries it again. For work that must not be lost, use a real job queue.
The examples below show a receipt sent after the response, several callables running in order, and an audit log sent with a time limit.
Parameters
$fncallable(): mixedThe function to run. It takes no arguments, and its return value is ignored. An error it throws is written to the log, and nocatchin the request sees it.
Options
deadlineCore\Time\DurationoptionalThe longest time$fnmay run. Without it, the[deferred] deadlinesetting is the limit. The request's[limits] wall_timedoes not apply here, but its other[limits]settings do.
Return value
Returns void. Nothing. The functions run one at a time, in the order you added them. They do not run if the request ends with an uncaught error, an exit or a fatal error.
Errors
RuntimeErrorWhen the call is made inside a task thatCore\Task::all,Core\Task::maporrunAfterResponsestarted. Only the request itself may call it, so return the work to the request and callrunAfterResponsethere.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Task::runAfterResponse runs a function after the response is sent. The old name was afterResponse. |
Examples
Send a receipt after the response
<?nvs// Shows how to send a receipt after the response. The customer sees the// confirmation first, and the receipt is sent after that.
string $email = 'anna@example.com';uint $order = 1042;
// This line adds the anonymous function to a list. It does not run it yet.Core\Task::runAfterResponse(fn(): void => { echo 'Receipt for order #', $order, ' sent to ', $email, "\n";});
// The response. This line prints before the receipt line.echo 'Thank you! Order #', $order, " is confirmed.\n";Thank you! Order #1042 is confirmed.Receipt for order #1042 sent to anna@example.comCallables run in the order you add them
<?nvs// Shows that the callables run one at a time, in the order you add them.
array<string> $pages = ['/', '/shop', '/contact'];
// Each anonymous function fills the cache for one page.// They print in the same order as the array: "/" first, "/contact" last.foreach ($pages as string $page) { Core\Task::runAfterResponse(fn(): void => { echo 'Cache filled for ', $page, "\n"; });}
echo "Response sent.\n";Response sent.Cache filled for /Cache filled for /shopCache filled for /contactSend the audit log with a time limit
<?nvs// Shows how to send an audit log after the response, with a time limit.
array<string> $audit = [];$audit[] = 'user 17 signed in';$audit[] = 'user 17 changed the email address';$audit[] = 'user 17 downloaded invoice 2026-031';
// `deadline: 10s` stops the anonymous function if it runs longer than 10 seconds.// A slow log server then cannot keep this work running for long.Core\Task::runAfterResponse(fn(): void => { foreach ($audit as string $line) { echo 'audit: ', $line, "\n"; }}, { deadline: 10s });
echo "Your settings are saved.\n";Your settings are saved.audit: user 17 signed inaudit: user 17 changed the email addressaudit: user 17 downloaded invoice 2026-031