solophp / router
A high-performance PHP router with middleware support, route groups, named routes, and advanced optional segment patterns.
v3.5.0
2026-10-16 20:19 UTC
Requires
- php: >=8.1
- solophp/contracts: ^1.5
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.5 || ^11.0 || ^12.0
- squizlabs/php_codesniffer: ^3.7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
High-performance PHP router with middleware support, route groups, named routes, and advanced optional segment patterns.
Features
- High Performance — Static routes use O(1) hash lookup, dynamic routes are prefiltered by literal prefix
- Fluent API — Chainable
->name(),->middleware()and->attribute()methods - Route Groups — Shared prefixes, middleware and attributes with nesting
- Middleware — Single and multiple middleware per route/group
- Advanced Patterns — Optional segments anywhere, nested optionals, regex constraints
- Named Routes — Easy route referencing for URL generation
- Route Attributes — Custom data such as permissions on routes and groups, returned by
match() - HEAD Support — Automatic fallback to GET routes per RFC 7231
- 405 Support —
getAllowedMethods()(AllowedMethodsInterfacefromsolophp/contracts) tells "method not allowed" from "not found"
Installation
composer require solophp/router
Quick Example
use Solo\Router\RouteCollector; $router = new RouteCollector(); // Simple routes $router->get('/users', [UserController::class, 'index']); $router->get('/users/{id}', [UserController::class, 'show']); // Route with middleware, name and attribute $router->post('/posts', [PostController::class, 'store']) ->middleware(AuthMiddleware::class) ->name('posts.store') ->attribute('permission', 'posts.create'); // Route groups $router->group('/admin', function(RouteCollector $router) { $router->get('/dashboard', [AdminController::class, 'dashboard']); $router->get('/users', [AdminController::class, 'users']); }, [AuthMiddleware::class], ['permission' => 'admin']); // Match request $match = $router->match('GET', '/users/123'); if ($match) { $handler = $match['handler']; $params = $match['params']; // ['id' => '123'] $middlewares = $match['middlewares']; $name = $match['name']; // route name or null $attributes = $match['attributes']; // custom route data, [] if none }
Documentation
- Installation
- Quick Start
- Handlers
- Route Parameters
- Optional Segments
- Route Groups
- Middleware
- Named Routes
- Route Attributes
- API Reference
Requirements
- PHP 8.1+
License
MIT License. See LICENSE for details.