Home > front end >  Laravel Inertia: switch rootView layout when redirecting
Laravel Inertia: switch rootView layout when redirecting

Time:04-19

I'm using Laravel Inertia React in my project and I define the rootView layout in the HandleInertiaRequests middleware, depending on which route is requested, like this:

  public function rootView(Request $request)
  {
    if ($request->route()->getPrefix() == "/admin") {
      return "adm";
    } else {
      return "app";
    }
  }

But I found a problem when redirecting from a route with app rootView to a route with adm rootView (from /login to /admin).

The problem is that the rootView is not changed before or after the redirect. Is there a way to force the redirection to reload the full page?

CodePudding user response:

I found the solution in this thread: https://stackoverflow.com/a/68609938/173299. The trick is add the following to the login request in AuthenticatedSessionController:

if ($request->session()->has("url.intended")) {
  return Inertia::location(session("url.intended"));
}

I leaving my original question just in case anyone else find it useful.

  • Related