akido-ld/simple-route
最新稳定版本:v1.1.2
Composer 安装命令:
composer require akido-ld/simple-route
包简介
A simple PHP routing library
README 文档
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
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-10-06