Example code (use in any controller extending the default Action):
$result = $this->resultRedirectFactory->create();
$result->setHttpResponseCode(303);
return $result->setPath('checkout/cart');
Why need this? Please describe a complete use case.
Why do we need a 303 status code? Surely that question answers itself.
Use case:
This is not a use case. For which purpose do you need such a status code?
There are a lot of codes with some $statusCode = $isPermanent ? 301 : 302 logic. To change this there should _really_ be a reason.
What you are asking is "why do you want to set a 303 response code?" which falls outside the remit of discussion in my opinion. Magento should be flexible enough to allow a custom response code, especially when the public API ($result->setHttpResponseCode()) implies that is allowed.
If you must know the use case, I am using XHR to send a PUT request, and then I want to send a 303 response code so it follows the redirect using GET instead.
Thanks for your input,
I am using XHR to send a PUT request, and then I want to send a 303 response code so it follows the redirect using GET instead
That was exactly what I was asking for.
Magento should be flexible enough to allow a custom response code
Any additional flexibility means more maintenance efforts. But of course ANY limitation/unimplemented feature should be reasonable.
After getting familiar with implementation it looks like https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Controller/ResultInterface.php#L13 is promising what you expect :)
In \Magento\Framework\Controller\AbstractResult::renderResult response code is set correctly first:
if (!empty($this->httpResponseCode)) {
$response->setHttpResponseCode($this->httpResponseCode);
}
but then suddenly rewritten in render:
$response->setRedirect($this->url);
Fix seems to be as simple as
if (!empty($this->httpResponseCode)) {
$response->setRedirect($this->url, $this->httpResponseCode);
} else {
$response->setRedirect($this->url);
}
Anybody want to create a PR? \Magento\Framework\Controller\Test\Unit\Result\RedirectTest needs to be expanded of course.
@jameshalsall, thank you for your report.
We've created internal ticket(s) MAGETWO-80996 to track progress on the issue.
I'm working on it #SQUASHTOBERFEST
Internal ticket to track issue progress: MAGETWO-81973
The issue has been fixed in 2.2-develop branch and will be available with 2.2.2 release
Hi @jameshalsall. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1286 by @magento-engcom-team in 2.3-develop branch
Related commit(s):
The fix will be available with the upcoming patch release.
Most helpful comment
Why do we need a 303 status code? Surely that question answers itself.
Use case: