Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Router Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Middleware Integration: Use Zend\Stratigility\MiddlewareRunner with a route-matching middleware that injects the router. Matched route params can be injected into downstream middleware via request attributes.
  • Named Route Linking: Use $router->assemble($name, $params) to generate URLs in templates or API responses—ensuring consistency when URLs change.
  • Modular Routing: Build isolated route stacks per module or feature and merge them using RouteStack::addRoutes()—ideal for reusable packages.
  • Constraint-Driven Matching: Leverage constraints on Segment routes for强类型 URL validation (e.g., UUIDs, dates) instead of relying on application logic.
  • Priority Trees: Register high-priority routes (e.g., health checks, admin paths) first; routes added later are checked last, avoiding overlap issues.

Gotchas and Tips

  • Finality of Matching: The router stops at the first matching route in the stack—even if later routes are more specific. Always order routes from most to least specific (e.g., /admin/users before /admin/:section).
  • Default Values Matter: Unset defaults (e.g., no action key) may cause downstream dispatchers to fail; use defaults for required variables to guarantee predictable matches.
  • URL Assembler Gotcha: assemble() doesn’t validate that params satisfy constraints—ensure your UI respects them (e.g., via form validation).
  • No PSR-7/15 Out-of-the-Box: Though framework-agnostic, integration with PSR-15 middleware requires wrapping it in an adapter. The project is archived (2019), so consider modern forks like laminas/laminas-router for active support.
  • Extending Routes: To support custom syntax (e.g., /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).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport