Write and access Routes
The way to access Laravel routes in React.
Executive Summary
- Register the route with a name in the route file.
- Call the function in react.
Use Case Scenarios
Full Tutorial
- Register the route in Laravel route file, giving it a name.
web.php
Route::get('/create', [TournamentController::class,'create'])->name('create');
- 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.
- 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]);
- You may retrieve the url by accessing the
.urlproperty.
edit-tournament-form.tsx
post(store(tournament).url, {
onSuccess: () => { resetAndClearErrors(); }
});