-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Muhammet Şafak edited this page Jun 9, 2026
·
2 revisions
Welcome to the official documentation for initphp/router — a fast,
framework-agnostic HTTP router for PHP. It turns an incoming
PSR-7 request into a response by matching
it against the routes you define, then running it through a middleware/handler
pipeline.
composer require initphp/routeruse InitPHP\HTTP\Message\{Request, Response};
use InitPHP\HTTP\Emitter\Emitter;
use InitPHP\Router\Router;
$router = new Router(Request::createFromGlobals(), new Response());
$router->get('/', fn () => 'Hello World!');
$router->get('/posts/{id}', fn ($id) => 'Post #' . $id);
(new Emitter())->emit($router->dispatch());The examples in this wiki use InitPHP HTTP for concreteness, but any PSR-7 request/response implementation works.
| Capability | Where to read |
|---|---|
HTTP verbs, register()/add(), any()
|
Routing |
| Typed & free-form path parameters, custom patterns | Route Parameters |
| Group routes by prefix, domain, port and client IP | Route Groups |
| Controller handlers and resource controllers | Controllers |
| Name routes and generate their URLs | Named Routes & URLs |
| Inject the request/response and services into handlers | Dependency Injection |
| Before/after middleware (filters) | Middleware |
| Serve files and directories | Static File Links |
| Custom 404 handling and the exception hierarchy | Error Handling · Exceptions |
| Cache the compiled route table | Caching |
- PHP 8.1+
- A PSR-7 HTTP message implementation and a PSR-7 emitter
- Optional: a PSR-11 container for service resolution
- Framework-agnostic — speaks PSR-7 in and out; drop it into any stack.
-
Expressive routing — typed parameters (
:id,:uuid, …), custom patterns, optional segments, and nested groups by prefix/domain/port/IP. -
Pipeline — before/after middleware per route and per controller, with a
clean halt mechanism (no
exit()). - Dependency injection — handler arguments are resolved by reflection, with optional PSR-11 container support.
- Typed & tested — fully typed, analysed at PHPStan's max level, and covered by an extensive test suite.
New here? Start with Installation and the Quick Start. Upgrading an existing project? See Upgrading 1.x → 2.0.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration