Convert Figma logo to code with AI

nikic logoFastRoute

Fast request router for PHP

5,090
445
5,090
21

Top Related Projects

7,569

Maps an HTTP request to a set of configuration variables

11,931

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

32,133

The Laravel Framework.

1,080

A lightweight and simple object oriented PHP Router

Quick Overview

FastRoute is a fast request router for PHP. It provides a simple and efficient way to map HTTP requests to callback functions, making it ideal for building web applications and APIs. FastRoute is designed to be lightweight and performant, with a focus on speed and simplicity.

Pros

  • Extremely fast and efficient routing
  • Simple and easy-to-use API
  • Supports various routing patterns, including static routes, placeholders, and regular expressions
  • Minimal dependencies and lightweight footprint

Cons

  • Limited built-in features compared to full-fledged frameworks
  • No built-in middleware support (though can be integrated with other libraries)
  • May require additional setup for more complex applications
  • Documentation could be more comprehensive

Code Examples

  1. Basic route definition:
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_details');
    $r->addRoute('POST', '/user', 'create_user');
});
  1. Handling a request:
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        // Handle 404 Not Found
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        // Handle 405 Method Not Allowed
        break;
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // Call $handler with $vars
        break;
}
  1. Using route groups:
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addGroup('/api', function (FastRoute\RouteCollector $r) {
        $r->addRoute('GET', '/users', 'get_all_users');
        $r->addRoute('POST', '/users', 'create_user');
    });
});

Getting Started

  1. Install FastRoute using Composer:

    composer require nikic/fast-route
    
  2. Create a new PHP file (e.g., index.php) and add the following code:

<?php
require 'vendor/autoload.php';

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/', function() {
        echo "Hello, World!";
    });
});

$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        call_user_func($handler, $vars);
        break;
    default:
        echo "404 Not Found";
        break;
}
  1. Run your PHP server and visit the root URL to see "Hello, World!" displayed.

Competitor Comparisons

7,569

Maps an HTTP request to a set of configuration variables

Pros of Symfony Routing

  • More feature-rich, offering advanced routing capabilities like route grouping and annotations
  • Part of the larger Symfony ecosystem, providing seamless integration with other Symfony components
  • Extensive documentation and community support

Cons of Symfony Routing

  • Generally slower performance compared to FastRoute, especially for large route sets
  • Higher memory usage due to its more complex architecture
  • Steeper learning curve for developers new to Symfony

Code Comparison

FastRoute:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_details');
});

Symfony Routing:

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

$routes = new RouteCollection();
$routes->add('get_all_users', new Route('/users', ['_controller' => 'get_all_users']));
$routes->add('get_user_details', new Route('/user/{id}', ['_controller' => 'get_user_details'], ['id' => '\d+']));
11,931

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Pros of Slim

  • Full-featured micro-framework with middleware support and dependency injection
  • Includes built-in PSR-7 implementation for HTTP message handling
  • Extensive documentation and large community support

Cons of Slim

  • Heavier and more complex than FastRoute for simple routing needs
  • Slower performance compared to FastRoute's lightweight design
  • Steeper learning curve for developers new to micro-frameworks

Code Comparison

FastRoute:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_details');
});

Slim:

$app = new \Slim\App();
$app->get('/users', function ($request, $response, $args) {
    // Handle get all users
});
$app->get('/user/{id}', function ($request, $response, $args) {
    // Handle get user details
});

FastRoute focuses solely on routing, while Slim provides a more comprehensive framework structure. FastRoute is ideal for projects requiring only routing functionality, whereas Slim is better suited for building complete web applications with additional features and middleware support.

32,133

The Laravel Framework.

Pros of Laravel Framework

  • Comprehensive full-stack framework with extensive features and tools
  • Large, active community providing support and resources
  • Built-in authentication, ORM, and routing systems

Cons of Laravel Framework

  • Heavier and more complex, potentially overkill for simple projects
  • Steeper learning curve for beginners
  • Higher resource consumption compared to lightweight alternatives

Code Comparison

FastRoute:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_details');
});

Laravel Framework:

Route::get('/users', 'UserController@index');
Route::get('/user/{id}', 'UserController@show')->where('id', '[0-9]+');

Summary

FastRoute is a lightweight, high-performance routing library, while Laravel Framework is a full-featured PHP web application framework. FastRoute excels in simplicity and speed for routing-specific tasks, whereas Laravel offers a complete ecosystem for building complex web applications. The choice between them depends on project requirements, scale, and developer preferences.

1,080

A lightweight and simple object oriented PHP Router

Pros of Router

  • Simpler API and easier to use for beginners
  • Built-in support for middleware
  • More flexible route matching patterns

Cons of Router

  • Less performant for large-scale applications
  • Fewer advanced features compared to FastRoute
  • Limited documentation and community support

Code Comparison

Router:

$router = new \Bramus\Router\Router();
$router->get('/users/{id}', function($id) {
    // Handle user request
});
$router->run();

FastRoute:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users/{id}', 'handler');
});
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);

Both libraries offer straightforward routing solutions for PHP applications. Router provides a more intuitive API and built-in middleware support, making it suitable for smaller projects or developers new to routing. FastRoute, on the other hand, offers better performance and more advanced features, making it a better choice for larger, more complex applications. The code comparison shows that Router has a slightly more concise syntax, while FastRoute requires a bit more setup but offers more flexibility in handling routes.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

FastRoute - Fast request router for PHP

Build Status

This library provides a fast implementation of a regular expression based router. Blog post explaining how the implementation works and why it is fast.

Install

To install with composer:

composer require nikic/fast-route

Requires PHP 8.1 or newer.

Usage

Here's a basic usage example:

<?php

require '/path/to/vendor/autoload.php';

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\ConfigureRoutes $r) {
    $r->addRoute('GET', '/users', 'get_all_users_handler');
    // {id} must be a number (\d+)
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
    // The /{title} suffix is optional
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        // ... 404 Not Found
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        break;
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // ... call $handler with $vars
        break;
}

Defining routes

The routes are defined by calling the FastRoute\simpleDispatcher() function, which accepts a callable taking a FastRoute\ConfigureRoutes instance. The routes are added by calling addRoute() on the collector instance:

$r->addRoute($method, $routePattern, $handler);

The $method is an uppercase HTTP method string for which a certain route should match. It is possible to specify multiple valid methods using an array:

// These two calls
$r->addRoute('GET', '/test', 'handler');
$r->addRoute('POST', '/test', 'handler');
// Are equivalent to this one call
$r->addRoute(['GET', 'POST'], '/test', 'handler');

By default, the $routePattern uses a syntax where {foo} specifies a placeholder with name foo and matching the regex [^/]+. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}. Some examples:

// Matches /user/42, but not /user/xyz
$r->addRoute('GET', '/user/{id:\d+}', 'handler');

// Matches /user/foobar, but not /user/foo/bar
$r->addRoute('GET', '/user/{name}', 'handler');

// Matches /user/foo/bar as well
$r->addRoute('GET', '/user/{name:.+}', 'handler');

Custom patterns for route placeholders cannot use capturing groups. For example {lang:(en|de)} is not a valid placeholder, because () is a capturing group. Instead you can use either {lang:en|de} or {lang:(?:en|de)}.

Furthermore, parts of the route enclosed in [...] are considered optional, so that /foo[bar] will match both /foo and /foobar. Optional parts are only supported in a trailing position, not in the middle of a route.

// This route
$r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler');
// Is equivalent to these two routes
$r->addRoute('GET', '/user/{id:\d+}', 'handler');
$r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler');

// Multiple nested optional parts are possible as well
$r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler');

// This route is NOT valid, because optional parts can only occur at the end
$r->addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler');

The $handler parameter does not necessarily have to be a callback, it could also be a controller class name or any other kind of data you wish to associate with the route. FastRoute only tells you which handler corresponds to your URI, how you interpret it is up to you.

Shortcut methods for common request methods

For the GET, POST, PUT, PATCH, DELETE and HEAD request methods shortcut methods are available. For example:

$r->get('/get-route', 'get_handler');
$r->post('/post-route', 'post_handler');

Is equivalent to:

$r->addRoute('GET', '/get-route', 'get_handler');
$r->addRoute('POST', '/post-route', 'post_handler');

Route Groups

Additionally, you can specify routes inside a group. All routes defined inside a group will have a common prefix.

For example, defining your routes as:

$r->addGroup('/admin', function (FastRoute\ConfigureRoutes $r) {
    $r->addRoute('GET', '/do-something', 'handler');
    $r->addRoute('GET', '/do-another-thing', 'handler');
    $r->addRoute('GET', '/do-something-else', 'handler');
});

Will have the same result as:

$r->addRoute('GET', '/admin/do-something', 'handler');
$r->addRoute('GET', '/admin/do-another-thing', 'handler');
$r->addRoute('GET', '/admin/do-something-else', 'handler');

Nested groups are also supported, in which case the prefixes of all the nested groups are combined.

Caching

The reason simpleDispatcher accepts a callback for defining the routes is to allow seamless caching. By using cachedDispatcher instead of simpleDispatcher you can cache the generated routing data and construct the dispatcher from the cached information:

<?php

$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\ConfigureRoutes $r) {
    $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
    $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
    $r->addRoute('GET', '/user/{name}', 'handler2');
}, [
    'cacheKey' => __DIR__ . '/route.cache', /* required */
    // 'cacheFile' => __DIR__ . '/route.cache', /* will still work for v1 compatibility */
    'cacheDisabled' => IS_DEBUG_ENABLED,     /* optional, enabled by default */
    'cacheDriver' => FastRoute\Cache\FileCache::class, /* optional, class name or instance of the cache driver - defaults to file cache */
]);

The second parameter to the function is an options array, which can be used to specify the cache key (e.g. file location when using files for caching), caching driver, among other things.

Dispatching a URI

A URI is dispatched by calling the dispatch() method of the created dispatcher. This method accepts the HTTP method and a URI. Getting those two bits of information (and normalizing them appropriately) is your job - this library is not bound to the PHP web SAPIs.

The dispatch() method returns an array whose first element contains a status code. It is one of Dispatcher::NOT_FOUND, Dispatcher::METHOD_NOT_ALLOWED and Dispatcher::FOUND. For the method not allowed status the second array element contains a list of HTTP methods allowed for the supplied URI. For example:

[FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']]

NOTE: The HTTP specification requires that a 405 Method Not Allowed response include the Allow: header to detail available methods for the requested resource. Applications using FastRoute should use the second array element to add this header when relaying a 405 response.

For the found status the second array element is the handler that was associated with the route and the third array element is a dictionary of placeholder names to their values. For example:

/* Routing against GET /user/nikic/42 */

[FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']]

Overriding the route parser and dispatcher

The routing process makes use of three components: A route parser, a data generator and a dispatcher. The three components adhere to the following interfaces:

<?php

namespace FastRoute;

interface RouteParser {
    public function parse($route);
}

interface DataGenerator {
    public function addRoute($httpMethod, $routeData, $handler);
    public function getData();
}

interface Dispatcher {
    const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;

    public function dispatch($httpMethod, $uri);
}

The route parser takes a route pattern string and converts it into an array of route infos, where each route info is again an array of its parts. The structure is best understood using an example:

/* The route /user/{id:\d+}[/{name}] converts to the following array: */
[
    [
        '/user/',
        ['id', '\d+'],
    ],
    [
        '/user/',
        ['id', '\d+'],
        '/',
        ['name', '[^/]+'],
    ],
]

This array can then be passed to the addRoute() method of a data generator. After all routes have been added the getData() of the generator is invoked, which returns all the routing data required by the dispatcher. The format of this data is not further specified - it is tightly coupled to the corresponding dispatcher.

The dispatcher accepts the routing data via a constructor and provides a dispatch() method, which you're already familiar with.

The route parser can be overwritten individually (to make use of some different pattern syntax), however the data generator and dispatcher should always be changed as a pair, as the output from the former is tightly coupled to the input of the latter. The reason the generator and the dispatcher are separate is that only the latter is needed when using caching (as the output of the former is what is being cached.)

When using the simpleDispatcher / cachedDispatcher functions from above the override happens through the options array:

<?php

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\ConfigureRoutes $r) {
    /* ... */
}, [
    'routeParser' => 'FastRoute\\RouteParser\\Std',
    'dataGenerator' => 'FastRoute\\DataGenerator\\MarkBased',
    'dispatcher' => 'FastRoute\\Dispatcher\\MarkBased',
]);

The above options array corresponds to the defaults. By replacing MarkBased with GroupCountBased you could switch to a different dispatching strategy.

A Note on HEAD Requests

The HTTP spec requires servers to support both GET and HEAD methods:

The methods GET and HEAD MUST be supported by all general-purpose servers

To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an available GET route for a given resource. The PHP web SAPI transparently removes the entity body from HEAD responses so this behavior has no effect on the vast majority of users.

However, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST NOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is your responsibility; FastRoute has no purview to prevent you from breaking HTTP in such cases.

Finally, note that applications MAY always specify their own HEAD method route for a given resource to bypass this behavior entirely.

Credits

This library is based on a router that Levi Morrison implemented for the Aerys server.

A large number of tests, as well as HTTP compliance considerations, were provided by Daniel Lowrey.