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 supports all standard HTTP methods, allowing you to build complete RESTful APIs. Each route can respond to specific HTTP methods, and the same URI can have different handlers for different methods.

Supported HTTP Methods

FastRoute provides helper methods for common HTTP verbs:
$r->get('/api/items', 'items_get');        // GET request
$r->post('/api/items', 'items_post');      // POST request
$r->delete('/api/test/delete/{id:\d+}', 'delete');  // DELETE request

Method-Specific Routes

The same URI path can respond differently based on the HTTP method:
$r->addRoute('GET', '/api/test', 'test_get');

function test_get() {
    echo json_encode(["message" => "This is the test route (GET)"]);
}

Complete Example

Here’s a complete example showing multiple HTTP methods:
routes/api.php
<?php

use FastRoute\RouteCollector;

return function (RouteCollector $r) {
    // GET requests
    $r->addRoute('GET', '/api/test', 'test_get');
    $r->addRoute('GET', '/api/items', 'items_get');
    
    // POST requests
    $r->addRoute('POST', '/api/test', 'test_post');
    $r->addRoute('POST', '/api/items', 'items_post');
    $r->post('/api/test/{test:\d+}', 'postdata');
    
    // DELETE requests
    $r->delete('/api/test/delete/{id:\d+}', 'delete');
};

Request Dispatching

The dispatcher automatically routes requests based on the HTTP method:
index.php
$method = $_SERVER['REQUEST_METHOD']; // Request method (GET, POST, etc.)

$dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r) {
    $apiRoutes = require __DIR__ . '/routes/api.php';
    $apiRoutes($r);
});

$routeInfo = $dispatcher->dispatch($method, $uri);

Method Not Allowed

FastRoute automatically handles cases where a route exists but the HTTP method is not allowed:
index.php
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        // Method not allowed
        header("HTTP/1.1 405 Method Not Allowed");
        echo json_encode(["message" => "Method not allowed"]);
        break;
}
This ensures proper HTTP status codes are returned when clients use incorrect methods.

Working with POST Data

Access POST data in your handlers using file_get_contents('php://input'):
function postdata($var) {
    echo $var['test'];
    $data = json_decode(file_get_contents('php://input'), true);
    echo json_encode($data);
}

HTTP Method Reference

Used to retrieve data from the server. Should not modify any data.
$r->get('/api/items', 'items_get');
Used to create new resources on the server.
$r->post('/api/items', 'items_post');
Used to delete resources from the server.
$r->delete('/api/test/delete/{id:\d+}', 'delete');
Generic method to add routes with any HTTP method.
$r->addRoute('GET', '/api/test', 'test_get');
$r->addRoute('POST', '/api/test', 'test_post');

RESTful Pattern

Follow RESTful conventions for HTTP methods:

GET

Retrieve data without side effects

POST

Create new resources

PUT

Update existing resources

DELETE

Remove resources

Best Practices

1

Use Appropriate Methods

Match HTTP methods to their semantic meaning (GET for retrieval, POST for creation, etc.)
2

Handle Method Not Allowed

Properly handle cases where routes exist but methods don’t match
3

Validate Input

Always validate and sanitize input data, especially for POST and PUT requests
4

Return Proper Status Codes

Use appropriate HTTP status codes (200, 201, 404, 405, etc.)

Next Steps

REST API

Build complete RESTful APIs with CRUD operations

Dynamic Parameters

Combine HTTP methods with dynamic route parameters

Build docs developers (and LLMs) love