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 offlinezero dependencieshttproutes
router_create(): Router — Creates a new Router with no base prefix.
router_create_with_prefix(string $prefix): Router — Creates a new Router with a normalized base path prefix.
router_normalize_path(string $path): string — Validates and normalizes a URL path (strips query string ?… and fragment #…, collapses //, removes trailing / except on root /).
router_join_paths(string $prefix, string $path): string — Joins a prefix and path into a single normalized path.
Router Class
Route Registration
All registration methods validate the method/pattern/handler and return $this for fluent chaining.
add(string $method, string $pattern, mixed $handler): Router Registers a route for any supported HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, or ANY).
get(string $pattern, mixed $handler): Router
post(string $pattern, mixed $handler): Router
put(string $pattern, mixed $handler): Router
delete(string $pattern, mixed $handler): Router
patch(string $pattern, mixed $handler): Router
any(string $pattern, mixed $handler): Router
Route Patterns
Static segments: /about, /api/v1/status
Named parameters: /users/:id/posts/:postId — captured in $match->params["id"] and $match->params["postId"].
Wildcard: /files/* — must be the final segment; captures zero or more remaining path segments into $match->params["*"].
Grouping & Mounting
beginGroup(string $prefix): Router — Pushes a path prefix onto the group stack (supports arbitrary nesting).
endGroup(): Router — Pops the most recent group prefix. Throws if called without a matching beginGroup().
mount(string $prefix, Router $subRouter): Router — Copies all routes from $subRouter under $prefix.
Resolution & Introspection
resolve(string $method, string $path): RouteMatch — Resolves an HTTP method and request path against registered routes in registration order.
routes(): array — Returns the array of registered route definitions.
count(): int — Returns the number of registered routes.
RouteMatch Class
Returned by Router::resolve().
Properties
bool $matched — true if a route matched both path and method.
int $status — 200 (matched), 404 (not found), or 405 (method not allowed).