Download the PHP package lachie4145/yaml-router without Composer
On this page you can find all versions of the php package lachie4145/yaml-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lachie4145/yaml-router
More information about lachie4145/yaml-router
Files in lachie4145/yaml-router
Package yaml-router
Short Description A lightweight YAML-configured HTTP router for PHP projects.
License MIT
Homepage https://github.com/lachie4145/yaml-router
Informations about the package yaml-router
YamlRouter
A lightweight YAML-configured HTTP router for PHP projects.
Define your routes in a routes.yaml file and let YamlRouter handle matching, dispatching, middleware, and more — no framework required.
Requirements
- PHP 8.1+
symfony/yaml^7.0 (installed automatically)
Installation
After install, a routes.example.yaml file is copied to your project root as a reference.
Quick Start
By default Router::create() reads routes.yaml from the current working directory. You can pass an explicit path:
Route File Format
Each route supports:
| Key | Required | Description |
|---|---|---|
method |
Yes | HTTP method (GET, POST, PUT, PATCH, DELETE) |
path |
Yes | URL path starting with / |
response |
One of these | Static response body |
handler |
One of these | PHP handler (see Handlers) |
redirect |
One of these | Redirect target URL |
name |
No | Named route identifier |
middleware |
No | String or list of middleware handlers |
constraints |
No | Per-parameter regex constraints |
status |
No | Override HTTP status code (default 200) |
headers |
No | Extra response headers |
Handlers
Static response
Controller method
The method receives array $params where keys are the dynamic path segments:
PHP file
The file is included with $params in scope. If the file echoes output it is captured; if it returns a value that value is used as the response.
Callable function
Route Groups
Group routes under a shared prefix, namespace, and/or middleware:
The prefix is prepended to every route path in the group. The namespace is prepended to controller-style handlers that don't already contain a namespace.
Constraints
Constrain dynamic parameters to a regex pattern:
Built-in aliases:
| Alias | Pattern |
|---|---|
int |
[0-9]+ |
slug |
[a-z0-9]+(?:-[a-z0-9]+)* |
uuid |
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} |
Named Routes
Assign a name to a route and generate URLs from it:
Middleware
Middleware handlers use the same ClassName@method or callable string format as route handlers. A middleware that returns a non-null value short-circuits dispatch and sends that value as the response instead. Returning null allows the request to continue.
Global middleware
Runs before every request:
Group middleware
Runs for every route in the group:
Per-route middleware
Middleware runs in order: global → group → per-route.
Redirects
The default redirect status is 302. Override with status:
Custom Status & Headers
Route Caching
Pass a cache file path to avoid re-parsing the YAML on every request:
The cache is automatically invalidated when routes.yaml is modified.
Built-in Route Docs
Enable a GET /docs endpoint that renders an HTML table of all registered routes:
Disable or guard this endpoint in production.
WAF
Enable the built-in Web Application Firewall to block common attack patterns:
Custom WAF rules can be added via customRules:
PSR-7 Support
Install psr/http-message in your project and use PsrAdapter:
License
MIT