akido-ld / simple-route
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
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^12.3
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
/examplesdirectory for more usage examples - See
/testsfor 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
๐ค Author
Akido LD GitHub: @AkidoLD