Skip to content

Commit f5282fc

Browse files
committed
Support for a custom URL resolver
1 parent 657d1e4 commit f5282fc

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Response.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,26 @@ class Response implements Responsable
3636

3737
protected $cacheFor = [];
3838

39+
protected ?Closure $urlResolver = null;
40+
3941
/**
4042
* @param array|Arrayable $props
4143
*/
42-
public function __construct(string $component, array $props, string $rootView = 'app', string $version = '', bool $encryptHistory = false)
43-
{
44+
public function __construct(
45+
string $component,
46+
array $props,
47+
string $rootView = 'app',
48+
string $version = '',
49+
bool $encryptHistory = false,
50+
?Closure $urlResolver = null
51+
) {
4452
$this->component = $component;
4553
$this->props = $props instanceof Arrayable ? $props->toArray() : $props;
4654
$this->rootView = $rootView;
4755
$this->version = $version;
4856
$this->clearHistory = session()->pull('inertia.clear_history', false);
4957
$this->encryptHistory = $encryptHistory;
58+
$this->urlResolver = $urlResolver;
5059
}
5160

5261
/**
@@ -365,6 +374,10 @@ public function isPartial(Request $request): bool
365374
*/
366375
protected function getUrl(Request $request): string
367376
{
377+
if ($this->urlResolver) {
378+
return App::call($this->urlResolver, ['request' => $request]);
379+
}
380+
368381
$url = Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/');
369382

370383
$rawUri = Str::before($request->getRequestUri(), '?');

src/ResponseFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ResponseFactory
3131

3232
protected $encryptHistory;
3333

34+
/** @var Closure|null */
35+
protected $urlResolver;
36+
3437
/***
3538
* @param string $name The name of the root view
3639
* @return void
@@ -93,6 +96,11 @@ public function getVersion(): string
9396
return (string) $version;
9497
}
9598

99+
public function resolveUrlUsing(?Closure $urlResolver): void
100+
{
101+
$this->urlResolver = $urlResolver;
102+
}
103+
96104
public function clearHistory(): void
97105
{
98106
session(['inertia.clear_history' => true]);
@@ -163,6 +171,7 @@ public function render(string $component, $props = []): Response
163171
$this->rootView,
164172
$this->getVersion(),
165173
$this->encryptHistory ?? config('inertia.history.encrypt', false),
174+
$this->urlResolver,
166175
);
167176
}
168177

tests/ResponseFactoryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ public function test_the_version_can_be_a_closure(): void
127127
$response->assertJson(['component' => 'User/Edit']);
128128
}
129129

130+
public function test_the_url_can_be_resolved_with_a_custom_resolver()
131+
{
132+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
133+
Inertia::resolveUrlUsing(function ($request, ResponseFactory $otherDependency) {
134+
$this->assertInstanceOf(HttpRequest::class, $request);
135+
$this->assertInstanceOf(ResponseFactory::class, $otherDependency);
136+
137+
return '/my-custom-url';
138+
});
139+
140+
return Inertia::render('User/Edit');
141+
});
142+
143+
$response = $this->withoutExceptionHandling()->get('/', [
144+
'X-Inertia' => 'true',
145+
]);
146+
147+
$response->assertSuccessful();
148+
$response->assertJson([
149+
'component' => 'User/Edit',
150+
'url' => '/my-custom-url',
151+
]);
152+
}
153+
130154
public function test_shared_data_can_be_shared_from_anywhere(): void
131155
{
132156
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {

0 commit comments

Comments
 (0)