akido-ld/simple-route

There is no license information available for the latest version (v1.1.2) of this package.

A simple PHP routing library

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/akido-ld/simple-route

v1.1.2 2025-10-18 10:38 UTC

This package is auto-updated.

Last update: 2025-12-18 11:11:27 UTC


README

A PHP routing library based on a tree structure

Why SimpleRoute?

Traditional PHP routers (like Laravel or Symfony) use flat arrays to define routes.

Problem: With nested routes, the code quickly becomes repetitive and harder to maintain:

// Classic router
$routes = [
    '/api/users' => $usersHandler,
    '/api/users/profile' => $profileHandler,
    '/api/users/settings' => $settingsHandler,
];

Solution: SimpleRoute uses a tree structure that naturally reflects the hierarchy of URLs.

You can build your route tree in two ways ๐Ÿ‘‡

๐Ÿงฑ Classic method (explicit)

$root->addChild($api);
$api->addChild($users);
$users->addChild($profile);
$users->addChild($settings);

โšก Simplified method (modern)

$root = new Node('root');
$api = new Node('api', parent: $root);
$users = new Node('users', parent: $api);
$profile = new Node('profile', parent: $users);
$settings = new Node('settings', parent: $users);

Result: Cleaner code, no duplication, and a clear visual hierarchy.

โœจ Features

  • ๐ŸŒณ Tree-based structure โ€“ Routes are organized hierarchically (parent/child), just like real URLs
  • โšก O(h) performance โ€“ Fast route lookup based on tree depth
  • โœ… 119 unit tests โ€“ Reliable code with high coverage
  • ๐Ÿง  Type-safe (PHP 8.1+) โ€“ Full type hints to prevent runtime errors
  • ๐Ÿงฉ Typed exceptions โ€“ Each error has its own class for easier debugging
  • ๐Ÿชถ Lightweight โ€“ Zero external dependencies

๐Ÿš€ Installation

Via Composer (recommended)

composer require akido-ld/simple-route

Manual installation (for contributors)

git clone https://github.com/AkidoLD/SimpleRoute.git
cd SimpleRoute
composer install

๐Ÿงฉ Usage Example

use SimpleRoute\Router\{Node, NodeTree, Router, UriSlicer};

// Create nodes
$root = new Node('root');
$api = new Node('api', parent: $root);
$users = new Node('users', function() {
    echo json_encode(['users' => ['Alice', 'Bob']]);
}, parent: $api);

// Router setup
$tree = new NodeTree($root);
$router = new Router($tree);

// Match an URL
$uri = new UriSlicer('/api/users');
$router->dispatch($uri);

// Output: {"users":["Alice","Bob"]}

๐Ÿ“š Documentation

  • Check the /examples directory for more usage examples
  • See /tests for detailed test cases and real-world usage patterns

๐Ÿงช Tests

./vendor/bin/phpunit

Stats: 119 tests โ€“ high coverage โœ…

To generate a coverage report:

XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html coverage

๐Ÿ“œ License

MIT License

๐Ÿ‘ค Author

Akido LD GitHub: @AkidoLD