solophp/router

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

Maintainers

Package info

github.com/SoloPHP/Router

pkg:composer/solophp/router

Statistics

Installs: 87

Dependents: 1

Suggesters: 1

Stars: 3

Open Issues: 0

v3.1.0 2026-03-11 21:53 UTC

This package is auto-updated.

Last update: 2026-03-11 21:56:01 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, pattern caching
  • Fluent API — Chainable ->name() and ->middleware() methods
  • Route Groups — Shared prefixes and middleware 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

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 and name
$router->post('/posts', [PostController::class, 'store'])
    ->middleware(AuthMiddleware::class)
    ->name('posts.store');

// Route groups
$router->group('/admin', function(RouteCollector $router) {
    $router->get('/dashboard', [AdminController::class, 'dashboard']);
    $router->get('/users', [AdminController::class, 'users']);
}, [AuthMiddleware::class]);

// 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
}

Documentation

Full Documentation

Requirements

  • PHP 8.1+

License

MIT License. See LICENSE for details.