Skip to main content

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:
  1. The route path and request URI are split by /
  2. Each segment is compared
  3. Segments wrapped in {} are captured as parameters
  4. All other segments must match exactly
The number of segments must match exactly. A route /users/{id} will not match /users/123/profile.

Parameter Names

Parameter names can be any valid variable name:

Using Parameters with Controllers

Parameters work seamlessly with controller methods:
The controller method receives parameters after the Request object:

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

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