Redirects
When making a non-GET Inertia request manually or via a <Link> element, you should ensure that you always respond with a proper Inertia redirect response.
For example, if your controller is creating a new user, your "store" endpoint should return a redirect back to a standard GET endpoint, such as your user "index" page. Inertia will automatically follow this redirect and update the page accordingly.
1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all(),7 ]);8 }910 public function store(Request $request)11 {12 User::create(13 $request->validate([14 'name' => ['required', 'max:50'],15 'email' => ['required', 'max:50', 'email'],16 ])17 );1819 return to_route('users.index');20 }21}1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all(),7 ]);8 }910 public function store(Request $request)11 {12 User::create(13 $request->validate([14 'name' => ['required', 'max:50'],15 'email' => ['required', 'max:50', 'email'],16 ])17 );1819 return to_route('users.index');20 }21}
303 Response Code
When redirecting after a PUT, PATCH, or DELETE request, you must use a 303 response code, otherwise the subsequent request will not be treated as a GETrequest. A 303 redirect is very similar to a 302 redirect; however, the follow-up request is explicitly changed to a GET request.
If you're using one of our official server-side adapters, all redirects will automatically be converted to 303 redirects.
External Redirects
Sometimes it's necessary to redirect to an external website, or even another non-Inertia endpoint in your app while handling an Inertia request. This can be accomplished using a server-side initiated window.location visit via the Inertia::location() method.
1return Inertia::location($url);1return Inertia::location($url);
The Inertia::location() method will generate a 409 Conflict response and include the destination URL in the X-Inertia-Location header. When this response is received client-side, Inertia will automatically perform a window.location = url visit.