feat: Add post_response event that gets triggered after response is sent to the client - #10423
feat: Add post_response event that gets triggered after response is sent to the client#10423patel-vansh wants to merge 7 commits into
post_response event that gets triggered after response is sent to the client#10423Conversation
…e event after sending response
There was a problem hiding this comment.
Pull request overview
This PR introduces a new lifecycle event, post_response, intended for running post-response work after the framework has sent the HTTP response to the client, enabling tasks like cleanup or analytics without delaying the response.
Changes:
- Trigger a new
post_responseevent fromCodeIgniter::run()aftersendResponse()completes. - Update
ResponseTrait::send()to finalize the response lifecycle (FastCGI/LiteSpeed finish request or output buffer flushing) before continuing execution. - Add system test coverage and user guide/changelog documentation for the new event.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| user_guide_src/source/extending/events.rst | Documents the new post_response event and its intent. |
| user_guide_src/source/changelogs/v4.8.0.rst | Adds a changelog entry for the new event (minor formatting nit noted). |
| tests/system/CodeIgniterTest.php | Adds a system test asserting post_response fires after the response is sent. |
| tests/_support/HTTP/Responses/ResponseWithPostSendFlag.php | Adds a test response subclass to detect when send() has completed. |
| system/HTTP/ResponseTrait.php | Finalizes/flushed response output after sending headers/cookies/body. |
| system/CodeIgniter.php | Triggers post_response after sending the response. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| - **Filters:** Added ``RequestId`` filter for request tracing and correlation logging. The filter stores the request ID in the request context and automatically adds the ``X-Request-ID`` response header. Incoming ``X-Request-ID`` headers are used when valid. See :ref:`requestid` for details. | ||
| - **Environment:** Added ``CodeIgniter\EnvironmentDetector`` class and corresponding ``environment`` service as a mockable wrapper around the ``ENVIRONMENT`` constant. | ||
| Framework internals that previously compared ``ENVIRONMENT`` directly now go through this service, making environment-specific branches reachable in tests via ``Services::injectMock()``. See :ref:`environment-detector-service`. | ||
| - **Events**: Added ``post_response`` event to run after the response is sent to the client. |
|
Copilot acted rather poorly here, so I asked Claude for the review: Problems
RecommendationMove the
Then add: a behavior-change note in the changelog, a SAPI-support note plus session-lock Minor: |
paulbalandan
left a comment
There was a problem hiding this comment.
I feel this should be 2 PRs: one for the event and one for the response finalization. If, however, it should be really be one PR then the title should be rephrased accurately.
| $level = count($status); | ||
| $flags = PHP_OUTPUT_HANDLER_REMOVABLE | PHP_OUTPUT_HANDLER_FLUSHABLE; | ||
|
|
||
| while ($level-- > 0 && ($s = $status[$level]) && ($s['del'] ?? (! isset($s['flags']) || ($s['flags'] & $flags) === $flags))) { |
There was a problem hiding this comment.
Can you destructure/rewrite this whole conditional line to be more readable?
|
Hi @paulbalandan and @michalsn, Due to a tight schedule, I won't be able to dedicate enough time to move this PR forward, so I'm closing it for now. If anyone would like to take over this feature, I'd be happy to leave it open for them. Otherwise, if I have time in the future and the feature hasn't already been implemented or another PR isn't in progress, I'll revisit it with the requested changes. |
Description
This PR adds a new
post_responseevent that is triggered from therun()method of the CodeIgniter class after the response has been sent to the client.The
ResponseTrait::send()method now manually finalizes the request lifecycle after sending the response. The implementation ofResponseTrait::finishResponse()andResponseTrait::closeOutputBuffers()is adapted from Symfony's implementation, which can be viewed here:This allows developers to register post-response tasks, such as cleanup operations or analytics processing, without delaying the response sent to the client.
Checklist: