Download the PHP package mycodebox/minirouter without Composer
On this page you can find all versions of the php package mycodebox/minirouter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package minirouter
MiniRouter
A minimalist, flexible PHP router for small and medium-sized projects.
Features: Named routes, middleware, groups, reverse routing, dependency injection, flexible response types, complex placeholders, forwarding, and more.
Table of Contents
- Overview & Features
- Installation
- Quick Start
- Routing & Placeholders
- Middleware
- Global Middleware
- Route Middleware
- Group Middleware
- Middleware as a Class
- Container
- Service Variants with MiniContainer
- Groups & API
- Request & Response
- Reverse Routing
- Error Handling
- Complete Demo
- API Overview
- License & Author
Overview & Features
- Routing for all HTTP methods (
GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD) - Placeholders & complex patterns (
/user/{id:\d+}) - Named routes & reverse routing (
urlFor) - Middleware (global, per route, per group, as class)
- Route groups with prefix and shared middleware
- Dependency injection via MiniContainer
- Automatic body parsing (JSON, form data)
- Flexible response types (text, JSON, HTML, download)
- Server-side forwarding
- Error handling & debug mode
Installation
- Copy the files from
src/into your project. - Install Composer dependencies (if needed).
- Create an entry point, e.g.,
index.php, or use the exampleexample/demo.php.
Quick Start
Tip:
MiniRouter supports middleware and dependency injection via container.
See examples in the next section.
Option 1: With Composer (recommended)
-
Install dependencies (only needed once):
- Example code:
Middleware
MiniRouter supports different types of middleware:
Global Middleware
Route Middleware
Group Middleware
Middleware as a Class
Container
Use the built-in container for dependency injection:
Service Variants with MiniContainer
You can register and use services in the container in various ways:
Variant 1: Anonymous Function (Closure)
Variant 2: Without Container Parameter
Variant 3: Using a Class
Variant 4: Using an Array as a Service
Variant 5: Explicit Service Factory
Routing & Placeholders
-
Simple route:
$router->get('/hello/{name}', $handler); -
Complex pattern:
$router->get('/user/{id:\d+}', $handler); // Numbers only -
Named route:
$router->get('/foo', $handler)->setName('foo_route'); -
Reverse routing:
$url = $router->urlFor('foo_route'); - ANY route (multi-method route):
How do placeholders and complex patterns work?
-
Simple placeholders:
Everything in{}is recognized as a variable and passed as a value in$argsto the handler.
Example:/hello/{name}matches/hello/World→$args['name'] === 'World' -
Complex patterns with regex:
After the name, a colon and a regular expression can follow, e.g.,{id:\d+}for numbers only.
Example:/user/{id:\d+}matches/user/42, but not/user/abc. -
Multiple placeholders:
You can use as many placeholders as you like, e.g.,/blog/{year:\d{4}}/{slug}. -
Default behavior:
Without regex, a placeholder accepts everything except/([^/]+). - Internal:
The router automatically converts the pattern into a regular expression and extracts the values as$args.
Examples:
Notes
- Case-insensitivity:
When registering HTTP methods (e.g., withany), case does not matter. You can mix'get','GET','Post', etc. - Quality:
The project is checked with PHPUnit tests and PHPStan.
Groups & API
Request & Response
- Request:
$req->method,$req->uri,$req->query,$req->body,$req->headers$req->getHeader('X-API-Key')$req->withAttribute('key', $value),$req->getAttribute('key')
- Response:
$res->withStatus(201)$res->withHeader('Content-Type', 'application/json')$res->withBody(['foo' => 'bar'])$res->send()
Reverse Routing
Error Handling
Complete Demo
A complete, practical example can be found in example/demo.php.
You can try the demo directly like this:
Then open in your browser, e.g., http://localhost:8080
The demo shows:
- Container setup
- Middleware (global, route, group, class)
- Named routes & reverse routing
- API group with auth middleware
- Various response types (text, JSON, HTML)
- Forwarding & error handling
API Overview
MiniRouter
addRoute($method, $pattern, $handler): MiniRouteget($pattern, $handler): MiniRoutepost($pattern, $handler): MiniRouteput($pattern, $handler): MiniRoutepatch($pattern, $handler): MiniRoutedelete($pattern, $handler): MiniRouteoptions($pattern, $handler): MiniRouteany(array $methods, string $pattern, $handler): arraygroup($prefix, $callback): MiniRouteGroupaddMiddleware($middleware): selfurlFor($name, $params = []): ?stringdispatch(): voidforward($path, $method = 'GET', $req = null, $res = null)getCurrentRoute(): ?MiniRoutepublic array $routes
MiniRoute
setName($name): selfgetName(): ?stringaddMiddleware($middleware): selfpublic string $patternpublic string $methodpublic array $middlewarepublic ?MiniRouteGroup $grouppublic $handlerpublic ?string $name
MiniRouteGroup
addMiddleware($middleware): selfaddRoute(MiniRoute $route): selfgetRoutes(): arraygetMiddleware(): arraypublic string $prefixpublic array $middlewarepublic array $routes
MiniRequest
public string $methodpublic string $uripublic array $querypublic mixed $bodypublic array $headerspublic array $attributesgetHeader($name): mixedwithAttribute($key, $value): selfgetAttribute($key): mixed
MiniResponse
withStatus($status): selfwithHeader($name, $value): selfwithBody($body): selfsend(): void
MiniContainer
set($name, $factory): voidget($name): mixedhas($name): bool
License & Author
License: MIT
Author: myCodebox