PHP code example of highperapp / router
1. Go to this page and download the library: Download highperapp/router library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
highperapp / router example snippets
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);
}
$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);
// 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');
$router->get('/admin', $handler)
->middleware('auth')
->middleware(['admin', 'cors']);
$router->get('/users/{id}', $handler)->name('user.show');
// Access route name
$match = $router->match($request);
$routeName = $match->getName();
$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'
]);
// Enable/disable caching (enabled by default)
$router->setCacheEnabled(true);
// Clear cache
$router->clearCache();
// Get statistics
$stats = $router->getStats();
// 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']);
}
// 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);
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);
}
}
// 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';
// 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'
)
*/
bash
# Unit Tests
php tests/Unit/RouterTest.php
# Integration Tests
php tests/Integration/RouterIntegrationTest.php
# Performance Tests
php bin/test-router-performance