Skip to content

Commit 9b0c7d3

Browse files
committed
CMS-2044 - Ensure redirects are forwarded to Laravel correctly
1 parent 5e4a0ff commit 9b0c7d3

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

yii2-adapter/src/Web/Response.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,26 @@ class Response extends \yii\web\Response implements Responsable
6565
*/
6666
private $_illuminateResponse;
6767

68+
private bool $_illuminateResponseInjected = false;
69+
70+
private bool $_illuminateResponsePrepared = false;
71+
6872
/**
6973
* @param bool $create whether to create a response, if it is empty.
7074
*/
7175
public function getIlluminateResponse(bool $create = false): ?SymfonyResponse
7276
{
73-
if ($create) {
74-
$this->_illuminateResponse ??= $this->createIlluminateResponse();
77+
if ($create && !$this->_illuminateResponsePrepared) {
78+
if ($this->_illuminateResponse === null) {
79+
$this->prepare();
80+
}
81+
82+
if (!$this->_illuminateResponseInjected && $this->_illuminateResponse !== null) {
83+
$this->sendHeaders();
84+
$this->sendContent();
85+
}
86+
87+
$this->_illuminateResponsePrepared = true;
7588
}
7689

7790
return $this->_illuminateResponse;
@@ -85,6 +98,8 @@ protected function createIlluminateResponse(): IlluminateResponse
8598
public function setIlluminateResponse(SymfonyResponse $response): static
8699
{
87100
$this->_illuminateResponse = $response;
101+
$this->_illuminateResponseInjected = true;
102+
$this->_illuminateResponsePrepared = true;
88103

89104
return $this;
90105
}
@@ -97,6 +112,8 @@ public function clear(): void
97112
parent::clear();
98113

99114
$this->_illuminateResponse = null;
115+
$this->_illuminateResponseInjected = false;
116+
$this->_illuminateResponsePrepared = false;
100117
}
101118

102119
/**

yii2-adapter/tests/unit/web/ResponseTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public function testRedirect(string $expected, mixed $url): void
9090
);
9191
}
9292

93+
public function testGetIlluminateResponseCopiesRedirectHeaders(): void
94+
{
95+
Craft::$app->getRequest()->setIsConsoleRequest(false);
96+
97+
$illuminateResponse = $this->response
98+
->redirect('/admin/stripe/settings')
99+
->getIlluminateResponse(true);
100+
101+
self::assertSame(302, $illuminateResponse->getStatusCode());
102+
self::assertSame('https://localhost/admin/stripe/settings', str_replace(':80', '', $illuminateResponse->headers->get('Location')));
103+
}
104+
93105
/**
94106
* @return array
95107
*/

0 commit comments

Comments
 (0)