router

HTTP/path URL router with named params, wildcards, route groups and 404/405.

v1.0.0Web & networkingbundledimport "router";
Overview Installation Documentation Examples

Overview

A zero-dependency HTTP/path URL router for HolyPHP with HTTP method matching, named route parameters, wildcard routes, route grouping/prefixing, sub-router mounting, and structured 404/405 match results.

bundled — works offline zero dependencies http routes

Quick start

import "router";

$r = router_create();

$r->get("/", "home_handler")
  ->get("/users/:id", "get_user")
  ->post("/users", "create_user")
  ->get("/files/*", "serve_file")
  ->any("/health", "health_check");

// Grouped routes with prefix
$r->beginGroup("/api/v1")
    ->get("/status", "api_status")
    ->delete("/sessions/:id", "revoke_session")
  ->endGroup();

$match = $r->resolve("GET", "/users/42?expand=profile");
if ($match->isMatched()) {
    println("Handler: " . $match->handler);
    println("User ID: " . $match->param("id"));
} else if ($match->isMethodNotAllowed()) {
    println("405: " . $match->error);
} else {
    println("404: " . $match->error);
}

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install router

This adds the library to hphp.yaml, installs it into .hphp/packages/router/ and pins the exact version in hphp.lock:

require:
  router: "^1.0"

Use it

import "router";

router is bundled with the compiler, so import "router"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install router          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install router
Packagerouter
Version1.0.0 (follows the compiler)
Entry filerouter.hphp
Sourcepackages/lib/router ↗

Documentation

API Reference

Helper Functions

Router Class

Route Registration

All registration methods validate the method/pattern/handler and return $this for fluent chaining.

Route Patterns

Grouping & Mounting

Resolution & Introspection

RouteMatch Class

Returned by Router::resolve().

Properties

Methods

Error Handling

All validation errors throw an Exception prefixed with router::

Examples

01Quick start

Bundled with the compiler — import "router"; works offline in any file, or add it to a project with hphp install router.

import "router";

$r = router_create();

$r->get("/", "home_handler")
  ->get("/users/:id", "get_user")
  ->post("/users", "create_user")
  ->get("/files/*", "serve_file")
  ->any("/health", "health_check");

// Grouped routes with prefix
$r->beginGroup("/api/v1")
    ->get("/status", "api_status")
    ->delete("/sessions/:id", "revoke_session")
  ->endGroup();

$match = $r->resolve("GET", "/users/42?expand=profile");
if ($match->isMatched()) {
    println("Handler: " . $match->handler);
    println("User ID: " . $match->param("id"));
} else if ($match->isMethodNotAllowed()) {
    println("405: " . $match->error);
} else {
    println("404: " . $match->error);
}
Try it
hphp install router
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_router.hphp.