zendframework/zend-router
Flexible HTTP router for Zend Framework, supporting literal/segment/regex paths, scheme, method, and hostname matching, with fast tree-based route combinations. Note: repository abandoned 2019-12-31; moved to laminas/laminas-router.
Start by installing the package via Composer: composer require zendframework/zend-router. Then, create a simple RouteStack and add basic routes—typically a Literal for static paths and a Segment for parameterized routes. For example:
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\Router\RouteStack;
$router = new RouteStack();
$router->addRoute('home', new Literal([
'route' => '/',
'defaults' => ['controller' => HomeController::class, 'action' => 'index'],
]));
$router->addRoute('user', new Segment([
'route' => '/user/:id',
'defaults' => ['controller' => UserController::class, 'action' => 'view'],
'constraints' => ['id' => '\d+'],
]));
Feed incoming $_SERVER data (e.g., request URI and method) into a ServerRequest and pass it to $router->match($request). The result contains matched route parameters (e.g., controller, action, id) ready for dispatch.
Zend\Stratigility\MiddlewareRunner with a route-matching middleware that injects the router. Matched route params can be injected into downstream middleware via request attributes.$router->assemble($name, $params) to generate URLs in templates or API responses—ensuring consistency when URLs change.RouteStack::addRoutes()—ideal for reusable packages.constraints on Segment routes for强类型 URL validation (e.g., UUIDs, dates) instead of relying on application logic./admin/users before /admin/:section).action key) may cause downstream dispatchers to fail; use defaults for required variables to guarantee predictable matches.assemble() doesn’t validate that params satisfy constraints—ensure your UI respects them (e.g., via form validation).laminas/laminas-router for active support./api/{v} for versioned routes), subclass Zend\Router\Http\Literal or write a custom RouteInterface and register via plugin manager (available if extending laminas-mvc-style setups).How can I help you explore Laravel packages today?