Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MonishAMPT/fastroute-code/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
Route parameters allow you to capture dynamic values from URLs. Instead of defining a separate route for every possible value, you can use parameter placeholders that match a pattern and extract the values for use in your handlers.Basic Parameter Syntax
Parameters are defined using curly braces{paramName} in your route pattern:
/user/123, /user/abc, /user/anything.
Regex Validation
FastRoute allows you to specify regex patterns to validate parameters using the syntax{paramName:pattern}:
/user/123 (digits), but not /user/abc.
Real-World Examples
Here are examples from the source code showing different parameter patterns:Multiple Parameters
Capture multiple dynamic values in a single route:api.php
/api/test/123/test/456, capturing:
id= 123id2= 456
Both parameters use
\d+ pattern, which matches one or more digits.DELETE with Parameter
Use parameters with different HTTP methods:api.php
DELETE /api/test/delete/42 and captures id = 42.
POST with Parameter
Combine parameters with POST requests:api.php
POST /api/test/789 and captures test = 789.
Accessing Parameters in Handlers
When a route with parameters is matched, FastRoute passes an array of parameters to your handler:index.php
$var['id']- First parameter$var['id2']- Second parameter
Single Parameter Handler
index.php
With POST Data
Combine route parameters with POST body data:index.php
Common Regex Patterns
| Pattern | Description | Example |
|---|---|---|
\d+ | One or more digits | Matches: 123, 45678 |
\w+ | One or more word characters | Matches: abc, test_123 |
[0-9]+ | One or more numbers | Matches: 123, 999 |
[a-z]+ | One or more lowercase letters | Matches: hello, world |
[a-zA-Z]+ | One or more letters | Matches: Hello, WORLD |
.+ | One or more of any character | Matches: anything |
Parameter Examples
User ID (Numeric Only)
Username (Alphanumeric)
Slug (Letters, numbers, hyphens)
Multiple Parameters Example
Here’s a detailed breakdown of the multi-parameter route:api.php
GET /api/test/42/test/99
Captured Parameters:
index.php
Optional Parameters
FastRoute doesn’t support optional parameters directly. Instead, define multiple routes:Best Practices
Always validate parameters
Always validate parameters
Use regex patterns to ensure parameters match expected formats. This prevents invalid data from reaching your handlers.
Use descriptive parameter names
Use descriptive parameter names
Name parameters based on what they represent:
{userId}, {postId}, {categorySlug} instead of generic names like {id} or {name}.Check array before accessing
Check array before accessing
Always verify
$var is an array before accessing parameters to handle routes without parameters gracefully.Keep patterns simple
Keep patterns simple
Use the simplest regex pattern that meets your requirements. Complex patterns are harder to maintain.
Next Steps
Route Groups
Combine parameters with route groups
Route Dispatcher
Understand how parameters are passed to handlers