Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heartshare/bdb319e47d887f23a91cadb178cc05b6 to your computer and use it in GitHub Desktop.
Save heartshare/bdb319e47d887f23a91cadb178cc05b6 to your computer and use it in GitHub Desktop.
inertia-laravel multiple root views
  1. Update or add the version and handle methods in your App\Http\Middleware\HandleInertiaRequests middleware.
  2. Add the middleware to the $routeMiddleware in your App\Http\Kernel.

Depending on your setup you might not want add the HandleInertiaRequests middelware directly to the web middleware stack.

Now you can add the middleware where needed and set the root template with inertia:[view].

class RouteServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->routes(function () {
            Route::middleware('web', 'inertia')
                ->group(base_path('routes/web.php'));

            Route::middleware('web', 'inertia:admin', 'auth')
                ->group(base_path('routes/admin.php'));
        });
    }
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Inertia\Middleware;
class HandleInertiaRequests 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.
*
* @see https://inertiajs.com/asset-versioning
* @param \Illuminate\Http\Request $request
* @return string|null
*/
public function version(Request $request)
{
// Add the root view tho the version hash.
// This forces inertia to make a hard realod when the template changes.
return $this->rootView . parent::version($request);
}
/**
* Defines the props that are shared by default.
*
* @see https://inertiajs.com/shared-data
* @param \Illuminate\Http\Request $request
* @return array
*/
public function share(Request $request)
{
return array_merge(parent::share($request), [
//
]);
}
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @return Response
*/
public function handle(Request $request, Closure $next)
{
if ($rootView = func_get_args()[2] ?? null) {
$this->rootView = $rootView;
}
return parent::handle($request, $next);
}
}
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'inertia' => \App\Http\Middleware\HandleInertiaRequests::class,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment