Skip to content

Instantly share code, notes, and snippets.

@lukaskleinschmidt
Last active June 3, 2024 03:54
Show Gist options
  • Save lukaskleinschmidt/714435e77e7b7a46f5960acaee54153a to your computer and use it in GitHub Desktop.
Save lukaskleinschmidt/714435e77e7b7a46f5960acaee54153a 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,
];
}
@Trial74
Copy link

Trial74 commented Jul 30, 2021

В файл HandleInertiaRequests.php необходимо добавить use Closure; иначе не находит класс Closure

@sleepm
Copy link

sleepm commented Jun 3, 2024

change HandleInertiaRequests.php line 56

if ($rootView = func_get_args()[2] ?? $this->rootView) {

laravel 11 bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias(['inertia' => \App\Http\Middleware\HandleInertiaRequests::class]);
});

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment