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

Basic routing in FastRoute allows you to define simple routes that respond to HTTP requests. Routes are defined using the RouteCollector object and can execute closures or reference handler functions.

Simple GET Routes

The most basic routes accept a URI and a closure that gets executed when the route is matched:
use FastRoute\RouteCollector;

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

    $r->get('/about', function() {
        require __DIR__ . '/../views/about.php';
    });
};
In this example:
  • The root route / loads the main view
  • The /about route loads the about view
  • Both routes respond only to GET requests

Route Files

Routes are typically organized in separate files:
<?php

use FastRoute\RouteCollector;

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

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

Key Concepts

Route Definition

Use $r->get(), $r->post(), or other HTTP method helpers to define routes

Closures

Routes can execute inline closures for simple logic

Modular Files

Organize routes in separate files and load them in your dispatcher

Clean Syntax

FastRoute provides an intuitive API for route definition

Next Steps

REST API

Build RESTful APIs with multiple HTTP methods

Dynamic Parameters

Use route parameters and regex validation

Build docs developers (and LLMs) love