Introduction
Route parameters allow you to capture dynamic segments from the URL and pass them to your route handlers. This is essential for building RESTful APIs and dynamic web applications.Basic Parameters
Define parameters in your route path using curly braces{}:
Route parameters are passed to your handler after the
Request object, in the order they appear in the route path.Multiple Parameters
You can define multiple parameters in a single route:How Parameter Matching Works
Lyger’s router uses a simple but powerful matching algorithm:- The route path and request URI are split by
/ - Each segment is compared
- Segments wrapped in
{}are captured as parameters - All other segments must match exactly
Parameter Names
Parameter names can be any valid variable name:Using Parameters with Controllers
Parameters work seamlessly with controller methods:Parameter Type Handling
All parameters are captured as strings. You should validate and cast them as needed:RESTful Resource Routes
Common pattern for RESTful APIs with parameters:Combining Parameters with Request Data
You can use both route parameters and request data together:Practical Examples
- User Profile
- File Download
- API Versioning
- Date-based Archives
Error Handling
When a route parameter doesn’t match your expectations, return appropriate error responses:Route Matching Priority
Routes are matched in the order they are defined. More specific routes should be defined before generic ones:Next Steps
Basic Routing
Back to basic routing concepts
Middleware
Add middleware to protect and process routes