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

FastRoute is a lightweight, high-performance PHP routing library that uses a dispatcher pattern to match incoming HTTP requests to registered routes. It provides a simple yet powerful API for defining routes and handling different HTTP methods.

How FastRoute Works

FastRoute operates on a three-step process:
  1. Route Registration: Define your routes using a RouteCollector
  2. Dispatcher Creation: Compile routes into an optimized dispatcher
  3. Route Matching: Match incoming requests and execute handlers

Creating the Dispatcher

The dispatcher is created using FastRoute\simpleDispatcher(), which accepts a callback function that receives a RouteCollector instance:
index.php
$dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r)  {
    // Load API routes
    $apiRoutes = require __DIR__ . '/routes/api.php';
    $apiRoutes($r);

    // Load web routes
    $webRoutes = require __DIR__ . '/routes/web.php';
    $webRoutes($r);
});
The dispatcher compiles all routes into an optimized data structure for fast lookups. This happens once during initialization.

Dispatching Requests

Once the dispatcher is created, you can match incoming requests by calling the dispatch() method with the HTTP method and URI:
index.php
$request_uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];

$uri = parse_url($request_uri, PHP_URL_PATH);

$routeInfo = $dispatcher->dispatch($method, $uri);
The dispatcher returns an array containing:
  • $routeInfo[0] - Status code (FOUND, NOT_FOUND, or METHOD_NOT_ALLOWED)
  • $routeInfo[1] - Handler function (when route is found)
  • $routeInfo[2] - Array of route parameters (when route is found)

Basic Route Registration

Routes are registered using the RouteCollector instance. Here are examples of basic routes:

Simple GET Routes

web.php
$r->get('/', function() {
    require __DIR__ . '/../views/main.php';
});

$r->get('/about', function() {
    require __DIR__ . '/../views/about.php';
});

Multiple HTTP Methods

You can register the same route for different HTTP methods:
api.php
$r->addRoute('GET', '/api/test', 'test_get');
$r->addRoute('POST', '/api/test', 'test_post');
$r->addRoute('GET', '/api/items', 'items_get');
$r->addRoute('POST', '/api/items', 'items_post');

Route Handlers

Handlers can be either:
  1. String callbacks: Function names that will be called
  2. Closures: Anonymous functions defined inline
api.php
// String callback
$r->get('/api/class', function() use($obj) {
    $obj->hello();
});

Organizing Routes

It’s a best practice to organize routes into separate files based on functionality (e.g., api.php for API routes, web.php for web routes). Each route file returns a function that accepts the RouteCollector:
web.php
return function (RouteCollector $r) {
    $r->get('/', function() {
        require __DIR__ . '/../views/main.php';
    });

    $r->get('/about', function() {
        require __DIR__ . '/../views/about.php';
    });
};
Separating routes by concern makes your application more maintainable and easier to navigate as it grows.

Next Steps

Route Dispatcher

Learn how the dispatcher handles different route outcomes

Route Parameters

Add dynamic parameters to your routes

Build docs developers (and LLMs) love