Skip to content

Commit fc48db7

Browse files
committed
Re-use redirect
1 parent 5ac8a8f commit fc48db7

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/ResponseFactory.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
use Illuminate\Support\Facades\Request;
1010
use Illuminate\Support\Traits\Macroable;
1111
use Illuminate\Contracts\Support\Arrayable;
12+
use Illuminate\Support\Facades\Redirect;
1213
use Illuminate\Support\Facades\Response as BaseResponse;
14+
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
15+
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;
1316

1417
class ResponseFactory
1518
{
@@ -103,32 +106,18 @@ public function render(string $component, $props = []): Response
103106
}
104107

105108
/**
106-
* @param string|RedirectResponse $url
109+
* @param string|SymfonyRedirect $url
107110
*/
108-
public function location($url): \Symfony\Component\HttpFoundation\Response
111+
public function location($url): SymfonyResponse
109112
{
110-
$request = $session = null;
111-
112-
if ($url instanceof RedirectResponse) {
113-
$request = $url->getRequest();
114-
115-
$session = $url->getSession();
116-
117-
$url = $url->getTargetUrl();
118-
}
113+
[$url, $redirect] = $url instanceof SymfonyRedirect
114+
? [$url->getTargetUrl(), $url]
115+
: [$url, Redirect::away($url)];
119116

120117
if (Request::inertia()) {
121118
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
122119
}
123120

124-
return tap(new RedirectResponse($url), function ($redirect) use ($request, $session) {
125-
if ($request) {
126-
$redirect->setRequest($request);
127-
}
128-
129-
if ($session) {
130-
$redirect->setSession($session);
131-
}
132-
});
121+
return $redirect;
133122
}
134123
}

tests/ResponseFactoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function test_location_response_for_non_inertia_requests_using_redirect_r
7878
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
7979
}
8080

81+
public function test_location_redirects_are_not_modified(): void
82+
{
83+
$response = (new ResponseFactory())->location('/foo');
84+
85+
$this->assertInstanceOf(RedirectResponse::class, $response);
86+
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
87+
$this->assertEquals('/foo', $response->headers->get('location'));
88+
}
89+
8190
public function test_location_response_for_non_inertia_requests_using_redirect_response_with_existing_session_and_request_properties(): void
8291
{
8392
$redirect = new RedirectResponse('https://inertiajs.com');
@@ -90,6 +99,7 @@ public function test_location_response_for_non_inertia_requests_using_redirect_r
9099
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
91100
$this->assertSame($session, $response->getSession());
92101
$this->assertSame($request, $response->getRequest());
102+
$this->assertSame($response, $redirect);
93103
}
94104

95105
public function test_the_version_can_be_a_closure(): void

0 commit comments

Comments
 (0)