diff --git a/src/Middleware.php b/src/Middleware.php index 1288a849..70775e10 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -8,6 +8,14 @@ class Middleware { + /** + * The root template that's loaded on the first page visit. + * + * @see https://inertiajs.com/server-side-setup#root-template + * @var string + */ + protected $rootView = 'app'; + /** * Determines the current asset version. * @@ -42,6 +50,18 @@ public function share(Request $request) ]; } + /** + * Sets the root template that's loaded on the first page visit. + * + * @see https://inertiajs.com/server-side-setup#root-template + * @param Request $request + * @return string + */ + public function rootView(Request $request) + { + return $this->rootView; + } + /** * Handle the incoming request. * @@ -57,6 +77,8 @@ public function handle(Request $request, Closure $next) Inertia::share($this->share($request)); + Inertia::setRootView($this->rootView($request)); + $response = $next($request); $response = $this->checkVersion($request, $response); $response = $this->changeRedirectCode($request, $response); diff --git a/stubs/middleware.stub b/stubs/middleware.stub index 97a5a3b0..596c1e1e 100644 --- a/stubs/middleware.stub +++ b/stubs/middleware.stub @@ -7,6 +7,14 @@ use Inertia\Middleware; class {{ class }} extends Middleware { + /** + * The root template that's loaded on the first page visit. + * + * @see https://inertiajs.com/server-side-setup#root-template + * @var string + */ + protected $rootView = 'app'; + /** * Determines the current asset version. * diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index 36bd8a4b..91b2b8aa 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -9,6 +9,7 @@ use Illuminate\Support\MessageBag; use Illuminate\Support\ViewErrorBag; use Inertia\Inertia; +use Inertia\Middleware; use Inertia\Tests\Stubs\ExampleMiddleware; class MiddlewareTest extends TestCase @@ -139,10 +140,39 @@ public function test_default_validation_errors_can_be_overwritten() ]); } - private function prepareMockEndpoint($version = null, $shared = []) + public function test_middleware_can_change_the_root_view_via_a_property() { - return Route::middleware(StartSession::class)->get('/', function (Request $request) use ($version, $shared) { - return (new ExampleMiddleware($version, $shared))->handle($request, function ($request) { + $this->prepareMockEndpoint(null, [], new class extends Middleware { + protected $rootView = 'welcome'; + }); + + $response = $this->get('/'); + $response->assertOk(); + $response->assertViewIs('welcome'); + } + + public function test_middleware_can_change_the_root_view_by_overriding_the_rootview_method() + { + $this->prepareMockEndpoint(null, [], new class extends Middleware { + public function rootView(Request $request) + { + return 'welcome'; + } + }); + + $response = $this->get('/'); + $response->assertOk(); + $response->assertViewIs('welcome'); + } + + private function prepareMockEndpoint($version = null, $shared = [], $middleware = null) + { + if (is_null($middleware)) { + $middleware = new ExampleMiddleware($version, $shared); + } + + return Route::middleware(StartSession::class)->get('/', function (Request $request) use ($middleware) { + return $middleware->handle($request, function ($request) { return Inertia::render('User/Edit', ['user' => ['name' => 'Jonathan']])->toResponse($request); }); });