Learn how to use Laravel route parameters to build dynamic, flexible URLs. Master required segments, optional parameters, and regex constraints today.
Previously in this course, we covered defining basic web routes, where we mapped static URLs to simple responses. Today, we take a leap forward by making those routes dynamic.
In real-world applications like our Task Manager, you rarely have static pages for everything. You need to identify specific resources—like showing a single task with an ID of 5 or filtering tasks by a category like work. This is where route parameters come in.
Route parameters allow you to capture segments of a URL and pass them into your route's logic. You define them by wrapping a name in curly braces {}.
Open your routes/web.php file and add this route:
PHPuse Illuminate\Support\Facades\Route; Route::get('/tasks/{id}', function ($id) { return 'Viewing task number: ' . $id; });
When you visit /tasks/10 in your browser, Laravel captures 10 from the URL, assigns it to the $id variable, and passes it into the closure.
You can define as many parameters as you need:
PHPRoute::get('/tasks/{taskId}/comments/{commentId}', function ($taskId, $commentId) { return "Task: {$taskId}, Comment: {$commentId}"; });
The order of arguments in your closure must match the order of the parameters in the route definition.
Sometimes a segment isn't always present. For example, you might want a route that works with or without a category filter. You define an optional parameter by adding a ? after the parameter name and providing a default value in the function signature.
PHPRoute::get('/tasks/{category?}', function ($category = 'all') { return "Showing tasks in category: " . $category; });
Now, visiting /tasks returns "Showing tasks in category: all", while /tasks/work returns "Showing tasks in category: work".
While capturing input is powerful, you often need to restrict what those segments look like to prevent invalid requests from hitting your application logic. We use the where method to enforce regex constraints.
If you want to ensure the {id} parameter is always numeric, you can chain the where method:
PHPRoute::get('/tasks/{id}', function ($id) { return 'Task ID: ' . $id; })->where('id', '[0-9]+');
If you visit /tasks/abc, Laravel will ignore this route and return a 404 error instead of trying to process "abc" as a task ID.
For multiple parameters, you can pass an array to the where method:
PHPRoute::get('/user/{name}/{id}', function ($name, $id) { #6A9955">// })->where([ 'name' => '[a-z]+', 'id' => '[0-9]+' ]);
In your routes/web.php file, create a new route for our Task Manager project that displays a task by its "slug" (a URL-friendly string) instead of an ID.
/tasks/view/{slug}.{slug} parameter only accepts alphabetic characters (a-z)./tasks/view/buy-groceries (it should fail the constraint) and /tasks/view/groceries (it should succeed).[a-z]+, it will break if your slugs contain hyphens (e.g., buy-groceries). Use [a-zA-Z0-9-_]+ for better flexibility./tasks/create and a dynamic route like /tasks/{id}, always define the static route first. Laravel matches routes in the order they are defined; otherwise, it might treat "create" as an ID.We've moved from static URLs to dynamic ones. By using curly braces, we capture URL segments; by adding a question mark, we make them optional; and by using where, we ensure our application only accepts valid, expected data. These tools are the foundation of clean, RESTful URL design in your Task Manager app.
Up next: Creating Your First Controller where we’ll move this logic out of our web.php file and into dedicated classes.
Master Laravel routing by learning how to map URLs to actions in web.php. This guide covers defining GET routes and returning responses for your app.
Read moreLearn how to create a TasksController and define a route for your Task Manager, moving from simple closures to a scalable MVC structure.
Using Route Parameters
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation