Routing
Defining Routes
When using Inertia, all of your application's routes are defined server-side. This means that you don't need Vue Router or React Router. Instead, you can simply define Laravel routes and return Inertia responses from those routes.
Shorthand Routes
If you have a page that doesn't need a corresponding controller method or handler, like an "FAQ" or "about" page, you can route directly to a component via Route::inertia()route.inertia().
1Route::inertia('/about', 'About');1Route::inertia('/about', 'About');
1use laravel::prelude::*;23pub fn routes(route: &mut Router) {4 route.inertia("/about", "About");5}1use laravel::prelude::*;23pub fn routes(route: &mut Router) {4 route.inertia("/about", "About");5}
Generating URLs
Some server-side frameworks allow you to generate URLs from named routes or handlers. However, you will not have access to those helpers client-side. Here are a couple ways to still generate route URLs with Inertia.
The first option is to generate URLs server-side and include them as props. Notice in this example how we're passing the edit_url and create_url to the Users/Index component.
1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all()->map(fn ($user) => [7 'id' => $user->id,8 'name' => $user->name,9 'email' => $user->email,10 'edit_url' => route('users.edit', $user),11 ]),12 'create_url' => route('users.create'),13 ]);14 }15}1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all()->map(fn ($user) => [7 'id' => $user->id,8 'name' => $user->name,9 'email' => $user->email,10 'edit_url' => route('users.edit', $user),11 ]),12 'create_url' => route('users.create'),13 ]);14 }15}
1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;3use serde_json::json;45use crate::models::User;67pub async fn index() -> laravel::Result<Page> {8 let users = User::all()9 .await?10 .into_iter()11 .map(|user| {12 Ok(json!({13 "id": user.id,14 "name": user.name,15 "email": user.email,16 "edit_url": route!(edit, &user)?,17 }))18 })19 .collect::<laravel::Result<Vec<_>>>()?;2021 Ok(inertia::render("Users/Index", props! {22 users,23 create_url => route!(create)?,24 }))25}1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;3use serde_json::json;45use crate::models::User;67pub async fn index() -> laravel::Result<Page> {8 let users = User::all()9 .await?10 .into_iter()11 .map(|user| {12 Ok(json!({13 "id": user.id,14 "name": user.name,15 "email": user.email,16 "edit_url": route!(edit, &user)?,17 }))18 })19 .collect::<laravel::Result<Vec<_>>>()?;2021 Ok(inertia::render("Users/Index", props! {22 users,23 create_url => route!(create)?,24 }))25}
When using Laravel PHP, you also have several options to make your server-side routes available to your client-side code:
Wayfinder
When using Wayfinder, you can pass the generated TypeScript method directly to the Link component, form helpers, or router methods and Inertia understands how to handle it. In fact, if you are developing an application using one of Laravel's starter kits, Wayfinder is already configured for you.
Ziggy
The Ziggy library can make your named, server-side routes available to you via a global route() function. If you're using the Vue plugin included with Ziggy, you may use the route() function directly in your templates.
1<Link :href="route('users.create')">Create User</Link>1<Link :href="route('users.create')">Create User</Link>
When server-side rendering is enabled, you may pass an options object to the Ziggy plugin in your ssr.js file. This should include the route definitions and current location.
1.use(ZiggyVue, {2 ...page.props.ziggy,3 location: new URL(page.props.ziggy.location),4});1.use(ZiggyVue, {2 ...page.props.ziggy,3 location: new URL(page.props.ziggy.location),4});
Customizing the Page URL
The page object includes a url that represents the current page's URL. By default, Inertia resolves this as a relative URL from the incoming request.
If you need to customize how the URL is resolved, you may provide a custom URL resolver in your HandleInertiaRequests middleware.
1class HandleInertiaRequests extends Middleware2{3 public function urlResolver()4 {5 return function (Request $request) {6 // Return the URL for the request...7 };8 }9}1class HandleInertiaRequests extends Middleware2{3 public function urlResolver()4 {5 return function (Request $request) {6 // Return the URL for the request...7 };8 }9}
1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.resolve_url_using(|request| {6 // Return the URL for the request...7 request.url().to_owned()8 })9 }10}1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.resolve_url_using(|request| {6 // Return the URL for the request...7 request.url().to_owned()8 })9 }10}
You may also define the resolver using the Inertia::resolveUrlUsing() method.
1Inertia::resolveUrlUsing(function (Request $request) {2 // Return the URL for the request...3});1Inertia::resolveUrlUsing(function (Request $request) {2 // Return the URL for the request...3});