Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Make inertia response more extension friendly #632

Open
wants to merge 7 commits into
base: 1.x
Choose a base branch
from
29 changes: 23 additions & 6 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ public function toResponse($request)
$props = $this->resolveAlwaysProps($props);
$props = $this->evaluateProps($props, $request);

$page = [
'component' => $this->component,
'props' => $props,
'url' => Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'),
'version' => $this->version,
];
$url = $this->resolveUrl($request, $props);
$page = $this->resolvePage($request, $props, $url);

if ($request->header(Header::INERTIA)) {
return new JsonResponse($page, 200, [Header::INERTIA => 'true']);
Expand Down Expand Up @@ -186,4 +182,25 @@ public function evaluateProps(array $props, Request $request, bool $unpackDotPro

return $props;
}

/**
* Resolve the URL for the response.
*/
public function resolveUrl(Request $request, array $props): string
{
return Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/');
}

/**
* Resolve the page for the response.
*/
public function resolvePage(Request $request, array $props, string $url): array
{
return [
'component' => $this->component,
'props' => $props,
'url' => $url,
'version' => $this->version,
];
}
}
12 changes: 6 additions & 6 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public function render(string $component, $props = []): Response
$props = $props->toArray();
}

return new Response(
$component,
array_merge($this->sharedProps, $props),
$this->rootView,
$this->getVersion()
);
return app(Response::class, [
'component' => $component,
'props' => array_merge($this->sharedProps, $props),
'rootView' => $this->rootView,
'version' => $this->getVersion(),
]);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Inertia\Tests\Stubs\ExampleMiddleware;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request as HttpRequest;
use Inertia\Response as InertiaResponse;
use Illuminate\Session\Middleware\StartSession;

class ResponseFactoryTest extends TestCase
Expand Down Expand Up @@ -195,4 +196,31 @@ public function toArray()
],
]);
}

public function test_it_allows_the_response_to_be_resolved_from_container(): void
{
app()->bind(InertiaResponse::class, function (): InertiaResponse {
return new class ('', []) extends InertiaResponse {
public function resolveUrl(HttpRequest $request, array $props): string
{
$requestedFrom = $request->header('x-requested-from');
$parsedRequestedFrom = parse_url($requestedFrom);

return "{$parsedRequestedFrom['path']}#{$parsedRequestedFrom['fragment']}";
}
};
});

Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
return Inertia::render('User/Edit');
});

$response = $this->withoutExceptionHandling()->get('/', [
'X-Inertia' => 'true',
'X-Requested-From' => 'https://example.com/abc/123#value',
]);

$response->assertSuccessful();
$response->assertJson(['url' => '/abc/123#value']);
}
}
Loading