Repository navigation
Conversation
redirect() and respond() carry the HTTP status in a pseudo "Status"
header, while the PSR-7 response kept status code 200. After
redirect($url, 303) getResponse()->getStatusCode() still returned 200,
and a status set later with withStatus() was ignored once a Status header
existed. respond() also appended its Cache-Control header to one set by
the application instead of replacing it.
setHeader('Status', ...) now also sets the status code of the response,
respond() sends the status line from the response status code, and the
Cache-Control header for uncacheable responses replaces an existing one,
like Expires and Last-Modified already do.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
redirect()andrespond()carry the HTTP status in a pseudoStatusheader instead of the PSR-7 status code. Afterredirect($url, 303),getResponse()->getStatusCode()is still 200, so PSR-7 consumers, emitters and middleware see the wrong status, and a later$app->setResponse($response->withStatus(404))is ignored once aStatusheader exists.respond()also adds itsCache-Controlheader with$replace = false, so an application-setCache-Controlis duplicated instead of replaced (twoCache-Controlheaders afterrespond()runs twice).This PR:
setHeader('Status', $code)(any case of the name) also callwithStatus()on the response for a numeric code between 100 and 599, so the header and the PSR-7 status code agree. This coversredirect()and every existingsetHeader('Status', …)call in applications,respond()send the status line fromgetStatusCode(): if theStatusheader differs (which can now only happen when the status was changed directly on the PSR-7 response), the header is replaced,Cache-Controlwith$replace = truefor uncacheable responses, likeExpiresandLast-Modifiedin the same branch.The finding also suggests deprecating the
Statusheader as a way to set the status. That is not done here: the docs and many applications (including the CMS) usesetHeader('Status', …), and with the synchronisation it no longer causes wrong status codes. Whether to deprecate it for 5.0 is open.Behaviour change in 4.x: with caching disabled, a
Cache-Controlheader set by the application is now replaced instead of being sent alongside the package's header. A status set on the PSR-7 response after aStatusheader now wins.Testing Instructions
New tests in
AbstractWebApplicationTest:testRespondUsesResponseStatusCode,testRespondReplacesCacheControlHeader,testSetHeaderStatusSetsResponseStatusCode, andtestRedirectWithMovednow also asserts that the response status code is 301. All four fail without the change insrc/; the existing redirect tests (which check the exact header order) still pass.Documentation Changes Required
docs/web-application.md: theStatusheader paragraph and the note under "Caching", which said thatCache-Controlwas appended to.docs/api-reference.md:setHeader().The "Caching" note is a few lines below the
Cache-Controlblock that #154 updates; they should merge cleanly, but whichever is merged second should be checked.🤖 Generated with Claude Code