Skip to content

Commit 2933f99

Browse files
committed
Added urlResolver() method to Middleware class
1 parent f5282fc commit 2933f99

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @method static void flushShared()
1414
* @method static void version(\Closure|string|null $version)
1515
* @method static string getVersion()
16+
* @method static void resolveUrlUsing(\Closure|null $urlResolver = null)
1617
* @method static \Inertia\OptionalProp optional(callable $callback)
1718
* @method static \Inertia\LazyProp lazy(callable $callback)
1819
* @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default')

src/Middleware.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ public function rootView(Request $request)
6969
return $this->rootView;
7070
}
7171

72+
/**
73+
* Defines a callback that returns the relative URL.
74+
*
75+
* @return Closure|null
76+
*/
77+
public function urlResolver()
78+
{
79+
return null;
80+
}
81+
7282
/**
7383
* Handle the incoming request.
7484
*
@@ -83,6 +93,10 @@ public function handle(Request $request, Closure $next)
8393
Inertia::share($this->share($request));
8494
Inertia::setRootView($this->rootView($request));
8595

96+
if ($urlResolver = $this->urlResolver()) {
97+
Inertia::resolveUrlUsing($urlResolver);
98+
}
99+
86100
$response = $next($request);
87101
$response->headers->set('Vary', Header::INERTIA);
88102

src/ResponseFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function getVersion(): string
9696
return (string) $version;
9797
}
9898

99-
public function resolveUrlUsing(?Closure $urlResolver): void
99+
public function resolveUrlUsing(?Closure $urlResolver = null): void
100100
{
101101
$this->urlResolver = $urlResolver;
102102
}

tests/MiddlewareTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inertia\AlwaysProp;
1212
use Inertia\Inertia;
1313
use Inertia\Middleware;
14+
use Inertia\Tests\Stubs\CustomUrlResolverMiddleware;
1415
use Inertia\Tests\Stubs\ExampleMiddleware;
1516
use LogicException;
1617

@@ -122,6 +123,21 @@ public function test_it_will_instruct_inertia_to_reload_on_a_version_mismatch():
122123
self::assertEmpty($response->getContent());
123124
}
124125

126+
public function test_the_url_can_be_resolved_with_a_custom_resolver()
127+
{
128+
$this->prepareMockEndpoint(middleware: new CustomUrlResolverMiddleware);
129+
130+
$response = $this->withoutExceptionHandling()->get('/', [
131+
'X-Inertia' => 'true',
132+
]);
133+
134+
$response->assertSuccessful();
135+
$response->assertJson([
136+
'component' => 'User/Edit',
137+
'url' => '/my-custom-url',
138+
]);
139+
}
140+
125141
public function test_validation_errors_are_registered_as_of_default(): void
126142
{
127143
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Inertia\Tests\Stubs;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\ResponseFactory;
7+
use Inertia\Middleware;
8+
use PHPUnit\Framework\Assert;
9+
10+
class CustomUrlResolverMiddleware extends Middleware
11+
{
12+
public function urlResolver()
13+
{
14+
return function ($request, ResponseFactory $otherDependency) {
15+
Assert::assertInstanceOf(Request::class, $request);
16+
Assert::assertInstanceOf(ResponseFactory::class, $otherDependency);
17+
18+
return '/my-custom-url';
19+
};
20+
}
21+
}

0 commit comments

Comments
 (0)