talira

FastRoute

Fast request router for PHP

open sourceBSD-3-Clausefree

FastRoute is a request router for PHP by Nikita Popov: it matches an HTTP method and URI against routes such as /user/{id:\d+} and returns the handler with the route's variables, or a not found or method not allowed result with the methods that are allowed. Routes take placeholders with custom regular expressions, optional trailing parts, shortcut methods for each HTTP method and nested groups with a common prefix, and the compiled routes can be cached to a file or another cache driver. Its speed comes from combining the routes into a few large regular expressions, which its author's blog post explains. It isn't tied to PHP's web server interfaces: the app passes in the method and URI.

Problems it solves

  • match incoming URLs to handlers in a PHP app without a full framework
  • a fast URL router for a PHP micro-framework of my own
  • extract route parameters like /user/{id} from a request path in PHP

Features

  • route placeholders with custom regular expressions and optional trailing parts
  • nested route groups with a shared prefix
  • shortcut methods for GET, POST, PUT, PATCH, DELETE and HEAD
  • cached route data through a file or another cache driver
  • returns the allowed methods for a 405 Method Not Allowed response
  • pluggable route parser and dispatcher