Download the PHP package rudra/router without Composer
On this page you can find all versions of the php package rudra/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
Rudra-Router
A lightweight and transparent HTTP router for PHP. Supports dynamic parameters, regular expressions, optional URL segments, middleware, and RESTful resources.
Features
- Dynamic URL parameters (
:name) and regular expressions (:[\d]{1,3}) - Optional URL segments with bracket syntax (
admin[/item[/page[/:page]]]) - Multiple URLs per route via arrays
- Attribute-based routing with
#[Routing]and#[Middleware] - All HTTP methods supported: GET, POST, PUT, PATCH, DELETE
- Method spoofing via
_method(for PUT/PATCH/DELETE from POST requests) - Middleware executed before and after controller execution
- RESTful resources in a single line
- Automatic dependency injection via IoC container
- Works via instance or Facade
Installation
Basic Usage
Initialization
Facade Usage
Route Definition
Simple Routes with Closure
With Regular Expressions
Controller Method Call
Via Facade
Attribute-based Routing
The most concise way to define routes — declare them directly on controller methods using PHP 8 attributes. Routes are discovered automatically via annotationCollector().
Basic Attribute Route
Optional URL Segments
Use square brackets [...] to mark segments as optional. Nested brackets are supported for deeply optional paths.
This single attribute expands into 4 separate routes at cache-build time:
| # | Expanded URL |
|---|---|
| 1 | admin |
| 2 | admin/item |
| 3 | admin/item/page |
| 4 | admin/item/page/:page |
Note: Expansion happens once during cache generation. At runtime the router works with flat strings — zero performance overhead.
Multiple URLs per Route
Pass an array of URLs to register several routes for a single method:
Combining Arrays and Optional Segments
Both features compose naturally — each element of the array is expanded independently:
This produces 8 routes (4 for admin/* + 4 for manager/*).
Regex Parameters Inside Routes
Regular expressions can be used freely — the bracket syntax for optional segments does not interfere with regex character classes:
Attributes with Middleware
Combine #[Routing] and #[Middleware] on the same method:
Preserving Route Parameters
All route parameters are preserved across expanded routes:
HTTP Methods
Any Method (GET|POST|PUT|PATCH|DELETE)
Middleware
Middleware runs before (before) and after (after) the controller. Each middleware must implement the __invoke() method.
Basic Setup
Middleware with Parameters
Middleware Example
Simple middleware without chain:
RESTful Resources
Registers standard CRUD routes with explicit plural and singular URL patterns. No magic pluralization — you define exactly what the URLs look like.
This creates the following routes:
| Method | URL | Controller Method | Description |
|---|---|---|---|
| GET | api/users | index | List all users |
| GET | api/user/:id | read | Get single user |
| POST | api/users | create | Create new user |
| PUT | api/user/:id | update | Full update user |
| PATCH | api/user/:id | update | Partial update user |
| DELETE | api/user/:id | delete | Delete user |
The default action names are [index, read, create, update, delete].
Custom Method Names
You can override the default action names by passing a custom array of 5 methods:
The array order is fixed: [index, read, create, update, delete].
The set() Method — Extended Syntax
Allows defining a route with multiple HTTP methods via |:
Controller Lifecycle
When a controller method is invoked, the following stages are executed:
shipInit()— base component initializationcontainerInit()— container initializationinit()— user-defined initializationbefore()— hook before middlewarebeforemiddleware- Action method call (with automatic dependency injection)
aftermiddlewareafter()— hook after middleware
Automatic Dependency Injection
Action method parameters are automatically resolved via the IoC container:
Method Spoofing
For forms that do not support PUT/PATCH/DELETE, use the _method parameter:
The router automatically recognizes this as a PUT request.
Error Handling
RouterException("Not Found", 404)— route not found or parameters do not matchRouterException("Service Unavailable", 503)— controller method does not existMiddlewareException— error in the middleware chain
License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0) — a free, open-source license that:
- Requires preservation of copyright and license notices,
- Allows commercial and non-commercial use,
- Requires that any modifications to the original files remain open under MPL-2.0,
- Permits combining with proprietary code in larger works.
📄 Full license text: LICENSE
🌐 Official MPL-2.0 page: https://mozilla.org/MPL/2.0/
All versions of router with dependencies
rudra/annotation Version self.version
rudra/container Version self.version
rudra/exception Version self.version