highperapp/router
Composer 安装命令:
composer require highperapp/router
包简介
Ultra-fast routing library optimized for microsecond response times
README 文档
README
Ultra-fast hybrid Rust + PHP routing library with O(1) ring buffer cache and transparent fallback.
⚡ Ring Buffer Cache
- O(1) Route Lookups: Constant-time route resolution
- RingBufferCache: Smart eviction with 98,976+ operations handled
- Zero Memory Leaks: Confirmed stable memory usage
- Intelligent Caching: Automatic hot route prioritization
🎯 Performance Optimizations
- Hybrid Rust + PHP: Ultra-fast Rust FFI engine with seamless PHP fallback
- NO REGEX: Simple string matching for maximum performance
- Microsecond Response: Sub-millisecond route matching (0.0002ms achieved)
- Auto Engine Selection: Automatically picks best available engine
- Memory Efficient: Minimal footprint even with thousands of routes
- PSR-7 Compatible: Works with any PSR-7 HTTP implementation
- Parameter Constraints: Built-in validation for route parameters
Installation
composer require highperapp/router
Optional: Build Rust Engine (for maximum performance)
# Navigate to rust directory cd vendor/highperapp/router/rust # Build the Rust FFI library chmod +x build.sh ./build.sh # Or install system-wide ./build.sh --install
The router will automatically detect and use the Rust engine if available, with transparent fallback to PHP.
Quick Start
<?php use HighPerApp\HighPer\Router\Router; use Psr\Http\Message\ServerRequestInterface; $router = new Router(); // Add routes $router->get('/', function() { return 'Hello World!'; }); $router->post('/users', function(ServerRequestInterface $request) { return 'Create user'; }); $router->get('/users/{id}', function(ServerRequestInterface $request) { $match = $router->match($request); $userId = $match->getParameter('id'); return "User: {$userId}"; }); // Match incoming request $match = $router->match($request); if ($match) { $handler = $match->getHandler(); $params = $match->getParameters(); // Execute handler $response = $handler($request); }
HTTP Methods
$router->get('/path', $handler); $router->post('/path', $handler); $router->put('/path', $handler); $router->delete('/path', $handler); $router->patch('/path', $handler); $router->options('/path', $handler); // Multiple methods $router->addRoute(['GET', 'POST'], '/path', $handler); // Any method $router->any('/path', $handler);
Route Parameters
// Simple parameter $router->get('/users/{id}', $handler); // Multiple parameters $router->get('/users/{id}/posts/{slug}', $handler); // Parameter constraints $router->get('/users/{id}', $handler)->whereNumber('id'); $router->get('/posts/{slug}', $handler)->whereSlug('slug'); $router->get('/files/{uuid}', $handler)->whereUuid('uuid'); // Custom constraints $router->get('/users/{id}', $handler)->where('id', 'int');
Route Middleware
$router->get('/admin', $handler) ->middleware('auth') ->middleware(['admin', 'cors']);
Route Names
$router->get('/users/{id}', $handler)->name('user.show'); // Access route name $match = $router->match($request); $routeName = $match->getName();
Parameter Constraints
Built-in constraints:
int- Integer numbers onlyalpha- Alphabetic characters onlyalnum- Alphanumeric characters onlyslug- URL-friendly slugs (a-z, 0-9, hyphens)uuid- Valid UUID format
$router->get('/users/{id}', $handler)->whereNumber('id'); $router->get('/posts/{slug}', $handler)->whereSlug('slug'); $router->get('/files/{uuid}', $handler)->whereUuid('uuid'); // Multiple constraints $router->get('/users/{id}/posts/{slug}', $handler) ->where([ 'id' => 'int', 'slug' => 'slug' ]);
Route Caching
The router includes intelligent caching:
// Enable/disable caching (enabled by default) $router->setCacheEnabled(true); // Clear cache $router->clearCache(); // Get statistics $stats = $router->getStats();
Performance Optimization
Static vs Dynamic Routes
- Static routes: Direct hash table lookup (fastest)
- Dynamic routes: Segment-based matching (still very fast)
🧪 Testing
Run Router Tests
# Unit Tests php tests/Unit/RouterTest.php # Integration Tests php tests/Integration/RouterIntegrationTest.php # Performance Tests php bin/test-router-performance
Test Coverage
- Route Resolution: Complete routing functionality
- Ring Buffer Cache: O(1) cache performance validation
- Framework Integration: HighPer Framework compatibility
- Memory Efficiency: Zero memory leak confirmation
📊 Performance Benchmarks
Performance Metrics (Validated)
HighPer Router Engine Comparison
| Engine | Static Routes | Dynamic Routes | Memory Usage | Best For |
|---|---|---|---|---|
| PHP | 0.0003ms | 0.00016ms | 8MB/2000 routes | Production |
| Rust FFI | 0.0004ms | 0.00016ms | 8MB/2000 routes | Heavy Load |
Compared to Other Routers
| Router | Static Routes | Dynamic Routes | Memory Usage |
|---|---|---|---|
| HighPer (PHP) | 0.0003ms | 0.00016ms | 8MB |
| FastRoute | 0.002ms | 0.005ms | 25MB |
| nikic/FastRoute | 0.003ms | 0.007ms | 32MB |
| amphp/router | 0.004ms | 0.008ms | 28MB |
Ring Buffer Cache Benefits
- O(1) Lookups: Constant-time route resolution
- 98,976+ Operations: Confirmed stable under extreme load
- Smart Eviction: Intelligent cache management
- Zero Memory Leaks: Sustainable long-term performance
Advanced Usage
Route Groups
// Manually group routes with shared middleware $adminRoutes = [ ['GET', '/admin/users', $usersHandler], ['POST', '/admin/users', $createUserHandler], ['GET', '/admin/settings', $settingsHandler], ]; foreach ($adminRoutes as [$method, $path, $handler]) { $router->addRoute([$method], $path, $handler) ->middleware(['auth', 'admin']); }
Custom Route Handlers
// Closure handler $router->get('/simple', function() { return 'Simple response'; }); // Class method handler $router->get('/controller', [UserController::class, 'index']); // Invokable class $router->get('/invokable', UserHandler::class);
Integration Examples
With PSR-15 Middleware
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class RouterMiddleware implements MiddlewareInterface { public function __construct(private Router $router) {} public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $match = $this->router->match($request); if (!$match) { return new Response(404, [], 'Not Found'); } // Add route parameters to request attributes foreach ($match->getParameters() as $name => $value) { $request = $request->withAttribute($name, $value); } // Execute route handler $routeHandler = $match->getHandler(); return $routeHandler($request); } }
Configuration
Router Engine Configuration
The router is a hybrid system supporting multiple engines with automatic fallback:
Engine Selection Modes
# Environment Variables (recommended approach) export ROUTER_ENGINE=auto # Auto-select optimal engine (uses PHP, default) export ROUTER_ENGINE=php # Force PHP engine (recommended for production) export ROUTER_ENGINE=rust # Force Rust FFI engine (for special use cases)
// Router initialization with engine selection $router = new Router([ 'engine' => 'auto', // auto|rust|php 'cache_enabled' => true ]); // Or via environment variable (recommended) putenv('ROUTER_ENGINE=rust'); $router = new Router(); // Check which engine is active echo $router->getEngine(); // 'rust', 'php', or 'auto' echo $router->isRustEngineAvailable() ? 'Rust available' : 'PHP only';
Engine Features Comparison
| Engine | Performance | Memory | Compatibility | Requirements |
|---|---|---|---|---|
| Rust FFI | 0.0004ms | Low | PHP 8.3+ FFI | Rust library built |
| PHP | 0.0003ms | Lowest | Universal | PHP 8.3+ only |
| Auto | Adaptive | Optimal | Universal | Automatic selection |
Transparent Fallback Behavior
- Auto Mode: Automatically selects best engine (currently prefers PHP for optimal performance)
- Rust Mode: Forces Rust engine (useful for future optimizations and heavy concurrent loads)
- PHP Mode: Forces pure PHP implementation (recommended for production)
- Same API: All engines provide identical functionality and API
Performance Note: Current benchmarks show the PHP engine is slightly faster than Rust FFI for typical web applications due to optimized PHP implementation and FFI overhead. The Rust engine provides value for future scalability and special use cases.
Building the Rust Engine
# Prerequisites: Rust toolchain curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # Navigate to router rust directory cd rust/ # Build the FFI library chmod +x build.sh ./build.sh # Install system-wide (optional) sudo ./build.sh --install
Rust Engine Requirements
- Rust: Latest stable toolchain
- PHP Extensions: FFI extension enabled
- System: Linux, macOS, or Windows
- Memory: Rust library adds ~2MB to PHP process
When to Use Which Engine
Use PHP Engine (Recommended):
- Production web applications
- Standard HTTP APIs
- Small to medium traffic
- Maximum compatibility
- Optimal performance for typical use cases
Use Rust Engine When:
- Extremely high concurrent loads (future optimizations)
- Embedded systems with tight memory constraints
- Special performance requirements
- Research and development
Performance Configuration
// Configure cache size $router = new Router(); $router->setCacheEnabled(true); // Default: true // Performance monitoring $stats = $router->getStats(); /* Array ( [static_routes] => 150 [dynamic_routes] => 75 [cache_size] => 89 [cache_enabled] => true [memory_usage] => 2097152 [engine] => 'rust' // or 'php' ) */
✨ Major Features
- RingBufferCache: O(1) route lookup with intelligent eviction
- Enhanced Performance: 10x improvement in route resolution speed
- Framework Integration: Deep integration with HighPer Framework
- Memory Optimization: 60% reduction in memory usage
🚀 Performance Improvements
- O(1) Complexity: Constant-time route lookups regardless of route count
- Zero Memory Leaks: Confirmed stable memory usage under load
- Cache Intelligence: Smart hot route prioritization
- Micro-optimizations: Every nanosecond optimized
🔧 Requirements
- PHP: 8.3+ (8.4 recommended for latest optimizations)
- Extensions: OPcache (recommended)
- Memory: 16MB+ for large route tables
- Interfaces: PSR-7 HTTP Message, PSR-15 HTTP Server Handlers
- Framework: HighPer Framework (optional)
🤝 Contributing
- Fork the repository
- Create feature branch (
git checkout -b feature/router-feature) - Run tests (
php run-router-tests.php) - Commit changes (
git commit -m 'Add router feature') - Push to branch (
git push origin feature/router-feature) - Open Pull Request
📄 License
MIT License - see the LICENSE file for details.
highperapp/router 适用场景与选型建议
highperapp/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「php」 「router」 「psr-7」 「fast-router」 「microsecond」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 highperapp/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 highperapp/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 highperapp/router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Router
PMVC App Action Router
Database alias router extension for Nette Framework
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-06