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

Dynamic parameters allow you to capture values from the URL and pass them to your handler functions. FastRoute supports parameter placeholders with optional regex validation.

Basic Parameter Syntax

Parameters are defined using curly braces {} with optional regex patterns:
$r->get('/api/test/{id:\d+}/test/{id2:\d+}', 'getid');
$r->delete('/api/test/delete/{id:\d+}', 'delete');
$r->post('/api/test/{test:\d+}', 'postdata');

Parameter Patterns

The syntax for parameters is {name:pattern} where:
  • name is the parameter name
  • pattern is an optional regex pattern
  • \d+ matches one or more digits
The colon (:) separates the parameter name from the regex pattern. If no pattern is provided, the parameter matches any value.

Accessing Parameters in Handlers

Parameters are passed to your handler function as an array:
function getid($var) {
    if (is_array($var)) {
        $id = $var['id'];
        $id2 = $var['id2'];
    }
    echo "given id: " . $id;
    echo "given id: " . $id2;
}

Complete Example

Here’s a complete example showing routes with parameters:
routes/api.php
<?php

use FastRoute\RouteCollector;

return function (RouteCollector $r) {
    // Multiple numeric parameters
    $r->get('/api/test/{id:\d+}/test/{id2:\d+}', 'getid');
    
    // Delete with ID parameter
    $r->delete('/api/test/delete/{id:\d+}', 'delete');
    
    // POST with parameter
    $r->post('/api/test/{test:\d+}', 'postdata');
};

Regex Patterns

Common regex patterns for route parameters:
// Matches only digits (e.g., 1, 123, 9999)
$r->get('/user/{id:\d+}', 'handler');

Handler Implementation

The dispatcher passes matched parameters to your handler:
index.php
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::FOUND:
        // Route found, call the handler
        $handler = $routeInfo[1];
        $var = $routeInfo[2];
        call_user_func($handler, $var);
        break;
}
Always validate and sanitize parameter values in your handlers, even when using regex patterns in routes.

Use Cases

Resource IDs

Capture numeric IDs for database lookups

Slugs

Use human-readable URLs with slug parameters

Multiple Params

Combine multiple parameters in a single route

Validation

Use regex patterns to validate input format

Next Steps

HTTP Methods

Learn about different HTTP method support

REST API

Build complete RESTful APIs

Build docs developers (and LLMs) love