sethrensei / ren-router
Secure lightweight PHP router with RBAC, CSRF and HTTP error handling
1.0.3
2026-04-02 12:55 UTC
Requires
- altorouter/altorouter: ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/log: ^3.0
README
RenRouter is a modern, lightweight and secure PHP microβrouter, designed for projects without a full framework or as the core of a custom microβframework.
It provides clean HTTP orchestration (routing, dispatching, views, errors), declarative security (authentication and roles), and a proper HTTP request abstraction.
β¨ Key Features
- HTTP routing powered by AltoRouter
- Fluent and readable route definitions
- Route protection with authentication and roles
- Centralized HTTP exception handling (401, 403, 404, 500)
- Dedicated error pages
- AJAX / Turbo / XHR support
- HTTP request abstraction (
Request) - Secure file uploads (
UploadedFile) - Optional PSRβ3 logger support
π§± Architecture
RenRouter/
βββ src/
β βββ Router.php
β βββ Security/
β β βββ Auth.php
β βββ Http/
β β βββ Request.php
β β βββ UploadedFile.php
β β βββ Exception/
β β βββ HttpException.php
β β βββ UnauthorizedHttpException.php
β β βββ ForbiddenHttpException.php
β β βββ NotFoundHttpException.php
βββ views/
β βββ base.php
β βββ errors/
β βββ 401.php
β βββ 403.php
β βββ 404.php
β βββ 500.php
βββ public/
βββ index.php
π Usage Example
$router->route( '/user/[i:id]', [$userController, 'show'], 'GET', 'user.show', [ 'auth' => true, 'roles' => ['admin', 'editor'] ] );
- The user must be authenticated
- The user must have at least one of the required roles
π Security & Roles
RenRouter follows a declarative security model:
- No authentication logic inside controllers
- Security rules are defined at route level
- A user may have one or multiple roles
['auth' => true, 'roles' => ['user']]
Automatically thrown exceptions:
| Situation | Exception | HTTP Code |
|---|---|---|
| Not authenticated | UnauthorizedHttpException | 401 |
| Invalid role | ForbiddenHttpException | 403 |
| Route not found | NotFoundHttpException | 404 |
β Error Handling
HTTP errors are centrally handled by the router and rendered using dedicated views:
views/errors/403.php
views/errors/404.php
The exception message is available in the view through $errorMessage.
π¦ Requirements
- PHP β₯ 8.1
fileinfoextension enabled- Composer
π― Philosophy
RenRouter focuses on:
- clarity over magic
- security by default
- a solid and extensible core
It is not a framework, but a reliable foundation to build one.
π License
MIT β free to use and modify.