Skip to main content

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:
$r->get('/user/{id}', 'get_user');
This will match /user/123, /user/abc, /user/anything.

Regex Validation

FastRoute allows you to specify regex patterns to validate parameters using the syntax {paramName:pattern}:
$r->get('/user/{id:\d+}', 'get_user');
This will only match /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
$r->get('/api/test/{id:\d+}/test/{id2:\d+}', 'getid');
This route matches URLs like /api/test/123/test/456, capturing:
  • id = 123
  • id2 = 456
Both parameters use \d+ pattern, which matches one or more digits.

DELETE with Parameter

Use parameters with different HTTP methods:
api.php
$r->delete('/api/test/delete/{id:\d+}', 'delete');
This matches DELETE /api/test/delete/42 and captures id = 42.

POST with Parameter

Combine parameters with POST requests:
api.php
$r->post('/api/test/{test:\d+}', 'postdata');
This matches 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
function getid($var){
    if (is_array($var)) {
        $id = $var['id'];
        $id2 = $var['id2'];
    }
    echo "given id: ".$id;
    echo "given id: ".$id2;
}
The parameter array uses parameter names as keys:
  • $var['id'] - First parameter
  • $var['id2'] - Second parameter

Single Parameter Handler

index.php
function delete($var){
    echo $var['id'];
}

With POST Data

Combine route parameters with POST body data:
index.php
function postdata($var){
    echo $var['test'];
    $data = json_decode(file_get_contents('php://input'), true);
    echo json_encode($data);
}
Always check if $var is an array before accessing parameters to avoid errors when routes have no parameters.

Common Regex Patterns

PatternDescriptionExample
\d+One or more digitsMatches: 123, 45678
\w+One or more word charactersMatches: abc, test_123
[0-9]+One or more numbersMatches: 123, 999
[a-z]+One or more lowercase lettersMatches: hello, world
[a-zA-Z]+One or more lettersMatches: Hello, WORLD
.+One or more of any characterMatches: anything

Parameter Examples

User ID (Numeric Only)

$r->get('/user/{id:\d+}', 'get_user');
// Matches: /user/123
// Doesn't match: /user/abc

Username (Alphanumeric)

$r->get('/profile/{username:[a-zA-Z0-9_]+}', 'get_profile');
// Matches: /profile/john_doe123
// Doesn't match: /profile/john-doe

Slug (Letters, numbers, hyphens)

$r->get('/post/{slug:[a-z0-9-]+}', 'get_post');
// Matches: /post/my-first-post
// Doesn't match: /post/My_Post

Multiple Parameters Example

Here’s a detailed breakdown of the multi-parameter route:
api.php
$r->get('/api/test/{id:\d+}/test/{id2:\d+}', 'getid');
Request: GET /api/test/42/test/99 Captured Parameters:
[
    'id' => '42',
    'id2' => '99'
]
Handler:
index.php
function getid($var){
    if (is_array($var)) {
        $id = $var['id'];      // '42'
        $id2 = $var['id2'];    // '99'
    }
    echo "given id: ".$id;     // given id: 42
    echo "given id: ".$id2;    // given id: 99
}

Optional Parameters

FastRoute doesn’t support optional parameters directly. Instead, define multiple routes:
$r->get('/user/{id:\d+}', 'get_user');
$r->get('/user', 'get_all_users');
Define more specific routes before less specific ones to ensure proper matching.

Best Practices

Use regex patterns to ensure parameters match expected formats. This prevents invalid data from reaching your handlers.
Name parameters based on what they represent: {userId}, {postId}, {categorySlug} instead of generic names like {id} or {name}.
Always verify $var is an array before accessing parameters to handle routes without parameters gracefully.
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

Build docs developers (and LLMs) love