Skip to content

Commit

Permalink
Merge pull request #167 from Cannonb4ll/master
Browse files Browse the repository at this point in the history
Ability to set root view on the new middleware
  • Loading branch information
reinink authored Oct 21, 2020
2 parents 947dcfd + b94a858 commit 0366204
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions stubs/middleware.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
36 changes: 33 additions & 3 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});
Expand Down

0 comments on commit 0366204

Please sign in to comment.