Route parameters allow you to capture values from a URL. You can use these values to display student details, course information, product records, and other dynamic content.
A route parameter is a placeholder inside a URL. Laravel extracts the value from the URL and passes it to the route callback or controller method.
Example:
Route::get('/student/{id}', function ($id) {
return 'Student ID: ' . $id;
});
Open this URL:
http://127.0.0.1:8000/student/101
Output:
Here, {id} is the route parameter,
and $id receives its value.
You can pass a student's name through the URL.
Route::get('/student-name/{name}', function ($name) {
return 'Welcome, ' . $name;
});
Example URL:
/student-name/Rahul
Output:
You can use this technique to create dynamic pages for students, teachers, products, or courses.
For real applications, validate input and escape user-provided values appropriately when displaying them in HTML.
A route can contain two or more parameters.
Route::get('/student/{id}/course/{course}', function ($id, $course) {
return 'Student ID: ' . $id . ', Course: ' . $course;
});
Example URL:
/student/101/course/PHP
Output:
Laravel passes the route parameter values to the callback in the order defined by the route.
An optional parameter allows a URL to work even when the parameter is not provided.
Route::get('/greeting/{name?}', function ($name = 'Student') {
return 'Hello, ' . $name;
});
URL 1:
/greeting
Output:
URL 2:
/greeting/Amit
Output:
Remember to use ? in the route
parameter and provide a default value in the
callback or controller method.
Sometimes a route should accept only numeric IDs.
Laravel provides whereNumber() for this.
Route::get('/student/{id}', function ($id) {
return 'Student ID: ' . $id;
})->whereNumber('id');
Matching URL:
/student/101
Non-matching URL:
/student/abc
The second URL does not match this route because the parameter is not numeric. If no other route matches, Laravel returns a 404 response.
Use route constraints to prevent invalid URL values from matching a route.
You can use where() to define a regular
expression for a route parameter.
Allow letters only:
Route::get('/category/{name}', function ($name) {
return 'Category: ' . $name;
})->where('name', '[A-Za-z]+');
This matches a category such as
/category/Books.
Allow digits only:
Route::get('/order/{id}', function ($id) {
return 'Order ID: ' . $id;
})->where('id', '[0-9]+');
This route accepts a numeric order ID such as
/order/500.
The route constraint controls which URL values match; it does not replace application-level validation or authorization.
You can pass a route parameter to a Blade view.
In routes/web.php:
Route::get('/course/{name}', function ($name) {
return view('course', ['courseName' => $name]);
});
Create the file
resources/views/course.blade.php.
Inside course.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Course Details</title>
</head>
<body>
<h1>Course Details</h1>
<p>Course Name: {{ $courseName }}</p>
</body>
</html>
Example URL:
/course/Laravel
The page displays the course name using Blade's
escaped output syntax, {{ }}.
For larger applications, place route-handling logic inside controllers rather than writing everything in a route closure.
Create a controller:
php artisan make:controller StudentController
Open app/Http/Controllers/StudentController.php
and add a method inside the controller class:
public function show($id)
{
return 'Student ID: ' . $id;
}
Register the route in routes/web.php:
use App\Http\Controllers\StudentController;
Route::get('/students/{id}', [StudentController::class, 'show'])
->whereNumber('id');
When a visitor opens /students/101,
Laravel calls the controller's show()
method and passes the ID.
Practise route parameters by creating the following
routes in routes/web.php.
/student/{id} to display a student ID./teacher/{name} to display a teacher's name./product/{id}/name/{name} to display a product ID and name./welcome/{name?} route with a default name.where() to allow letters only in a category name.php artisan route:list and inspect your routes.Test both valid and invalid URLs. Make sure each route has a unique path or an intentional constraint so that routes do not conflict.
1. What does the parameter {id} represent in a route?
2. Which symbol makes a route parameter optional?
3. Which method restricts a route parameter to numbers?