Skip to content

Instantly share code, notes, and snippets.

@lukaskleinschmidt
Last active September 25, 2023 21:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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

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