Search by

solophp / router

Vitaliy Olos

A high-performance PHP router with middleware support, route groups, named routes, and advanced optional segment patterns.

Package info

github.com/SoloPHP/Router

pkg:composer/solophp/router

Statistics

Installs: 146

Dependents: 2

Suggesters: 1

Stars: 3

Open Issues: 0

v3.5.0 2026-10-16 20:19 UTC

This package is auto-updated.

Last update: 2026-10-16 20:22:12 UTC


README

High-performance PHP router with middleware support, route groups, named routes, and advanced optional segment patterns.

Latest Version on Packagist PHP Version License

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() (AllowedMethodsInterface from solophp/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

Full Documentation

Requirements

  • PHP 8.1+

License

MIT License. See LICENSE for details.