Skip to main content

Write and access Routes

The way to access Laravel routes in React.

Executive Summary

  1. Register the route with a name in the route file.
  2. Call the function in react.

Use Case Scenarios

  1. Links
  2. Forms
  3. Manual visits

Full Tutorial

  1. Register the route in Laravel route file, giving it a name.
web.php
Route::get('/create', [TournamentController::class,'create'])->name('create');
  1. Import the function based on the name of the route, then call it.
tournament-index.tsx
import { create } from '@/routes/tournaments';
tournament-index.tsx
<Link href={create()}>Create Tournament</Link>
warning

If the route is named delete or import, the name of the function will be deleteMethod or importMethod instead.

  1. To insert a route parameter, pass it to the function.
show-tournament.tsx
<Link href={edit(tournament.id)}>Edit</Link>
info

If there are more than one parameter, use an array.

update([1,2]);
  1. You may retrieve the url by accessing the .url property.
edit-tournament-form.tsx
post(store(tournament).url, {
onSuccess: () => { resetAndClearErrors(); }
});

Extra Materials

  1. Laravel Wayfinder