Download the PHP package genai/routing without Composer
On this page you can find all versions of the php package genai/routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download genai/routing
More information about genai/routing
Files in genai/routing
Package routing
Short Description Minimal PSR-4 standalone route matching engine (regexes baked at build, no runtime compilation), compatible with PHP 5.3.29. Attribute-driven routing (#[Route]) lives in genai/web.
License Apache-2.0
Informations about the package routing
GenAI Routing
A minimal, dependency-free pure route matching engine for PHP, built and tested against PHP 5.3.29.
You give it an HTTP method and a request path; it matches them against your configured routes and returns the handler (e.g. UserController@show) together with any captured parameters. It does not read the request for you and it does not dispatch — it only answers the question "which controller/action handles this request?".
Features
- Match by HTTP method + path → returns handler + params, or
falseon no match (your 404 signal). - Named placeholders:
/user/{id}. - Optional regex constraints:
/user/{id:\d+}. - Multiple placeholders per route:
/posts/{slug}/comments/{commentId}. - Per-method helpers (
get,post,put,patch,delete) plusany(). - Bulk loading of a whole route table in one call via
load()— ideal for a route table built or cached once at boot. - Handler splitting helpers:
getController()/getAction()forController@actionstrings. - PSR-4 namespaced (
GenAI\Routing\), with both a Composer mapping and a standalone autoloader. - 100% PHP 5.3.29 compatible —
array()syntax only, no features newer than 5.3.
Requirements
- PHP >= 5.3.0 (verified on 5.3.29).
- No extensions beyond core PCRE.
Installation
Option A — standalone (no Composer)
Require the bundled autoloader (see demo/autoload.php for a ready-made one):
Option B — Composer
A PSR-4 mapping is already declared in composer.json:
Run composer dump-autoload, then use Composer's autoloader as usual.
Quick start
Loading a whole route table at once
When you collect all controller/action pairs at boot, hand the entire table to the
router with load(). Both entry shapes are accepted, so it fits whatever your
compile step produces:
Caching the route table
The table is a plain array, so you can build it once and cache it to disk with
var_export(), skipping the collection work on every request:
Route patterns
| Pattern | Matches | Captured params |
|---|---|---|
/users |
/users |
(none) |
/user/{id} |
/user/5, /user/abc |
array('id' => '5') |
/user/{id:\d+} |
/user/5 (not /user/abc) |
array('id' => '5') |
/posts/{slug}/comments/{commentId} |
/posts/hello/comments/9 |
array('slug' => 'hello', 'commentId' => '9') |
Notes:
{name}matches a single path segment ([^/]+).{name:regex}constrains the segment with your own regex.- Literal parts of the pattern are escaped, so a
.in a URL matches a literal dot. - Trailing slashes are normalized:
/users/is treated the same as/users.
Matching rules
- First match wins. Register/emit more specific routes before more general
ones (e.g.
/user/{id}/editbefore/user/{id}). - Method comparison is case-insensitive (
get==GET). - A route registered with
any()(method*) matches every HTTP method. match()returns aRouteMatchon success, orfalsewhen nothing matched.
API reference
GenAI\Routing\Router
| Method | Description |
|---|---|
add($method, $pattern, $handler) |
Register one route. Returns $this. |
get($pattern, $handler) |
Shortcut for add('GET', ...). |
post($pattern, $handler) |
Shortcut for add('POST', ...). |
put($pattern, $handler) |
Shortcut for add('PUT', ...). |
patch($pattern, $handler) |
Shortcut for add('PATCH', ...). |
delete($pattern, $handler) |
Shortcut for add('DELETE', ...). |
any($pattern, $handler) |
Register a route matching any method. |
load(array $routes) |
Register many routes from a single config array. Returns $this. |
match($method, $path) |
Returns a RouteMatch, or false if nothing matched. |
GenAI\Routing\RouteMatch
| Method | Description |
|---|---|
getHandler() |
The raw handler as registered (e.g. 'UserController@show'). |
getParams() |
All captured params, e.g. array('id' => '5'). |
getParam($name, $default = null) |
A single param, or $default if absent. |
getController() |
Controller part of a Controller@action handler. |
getAction() |
Action part of a Controller@action handler. |
Project layout
Running the example
With a local PHP:
Against a real PHP 5.3.29 via Docker (no local PHP needed):
Expected output:
PHP 5.3 compatibility notes
The codebase deliberately stays within PHP 5.3:
array()syntax everywhere (no[]short array syntax — that is 5.4+).- Namespaces and closures (both available in 5.3).
- Named regex groups
(?P<name>...). - Avoids everything newer:
[],??,?:-as-shorthand pitfalls,::class, scalar type hints, traits, and variadics.