Laravel Route Parameters
A route parameter is a value taken from the web address. The address carries the value, and the route hands it to the closure.
One value in the address
Visit /l07/hello/Sanjay to greet Sanjay.
Route::get('/l07/hello/{name}', fn (string $name) => "Hello, {$name}!");A number in the address
Visit /l07/user/42 to see user 42.
Route::get('/l07/user/{id}', fn (string $id) => "User {$id}");Two values
Visit /l07/post/7/comment/3 to see post 7 and comment 3.
Route::get('/l07/post/{post}/comment/{comment}',
fn (string $post, string $comment) => "Post {$post}, comment {$comment}");Example
<?php
use Illuminate\Support\Facades\Route;
Route::get('/l07/hello/{name}', fn (string $name) => "Hello, {$name}!");Key points
- Route parameter: a value taken from the web address.
- Curly braces mark a parameter in the address, like {name}.
- The closure receives the value as a function argument.
- One route can serve many addresses with different values.
- Use a parameter when the page changes with one value.
In this example, the first lines load the Route helper. The last line adds a route with a {name} parameter. A visit to /l07/hello/Sanjay sees Hello, Sanjay!
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What marks a route parameter in the address?