VOOZH about

URL: https://deepwiki.com/hypervel/router/2-core-routing-components

⇱ Core Routing Components | hypervel/router | DeepWiki


Loading...
Menu

Core Routing Components

Purpose and Scope

This document provides an overview of the three core routing components in hypervel/router that work together to handle route definition, collection, and dispatching. These components form the foundation of the routing system:

  • Router - Primary API for defining routes and managing parameter bindings
  • RouteCollector - Collects and organizes route definitions with middleware and naming
  • DispatcherFactory - Initializes routes and compiles them into executable dispatchers

For detailed information about specific aspects of these components, see:

For URL generation capabilities, which consume the routes defined by these components, see URL Generation System.


Three-Component Architecture

The routing system is organized into three collaborating components, each with distinct responsibilities:


Sources: src/Router.php1-189 src/RouteCollector.php1-183 src/DispatcherFactory.php1-51


Component Responsibilities

ComponentPrimary RoleKey Data StructuresFile Reference
RouterRoute definition API and parameter binding registrymodelBindings array, explicitBindings arraysrc/Router.php
RouteCollectorRoute collection, organization, and storagenamedRoutes array, FastRoute dataGeneratorsrc/RouteCollector.php
DispatcherFactoryRoute initialization, compilation, and cachingrouters array (per-server collectors), initialized flagsrc/DispatcherFactory.php

Router Class

The Router class (src/Router.php20) serves as the primary entry point for route definition. It provides:

RouteCollector Class

The RouteCollector class (src/RouteCollector.php13) extends Hyperf's base collector and adds:

DispatcherFactory Class

The DispatcherFactory class (src/DispatcherFactory.php13) extends Hyperf's base factory and adds:

Sources: src/Router.php20-189 src/RouteCollector.php13-183 src/DispatcherFactory.php13-51


Route Definition to Dispatch Flow

The following sequence illustrates how a route travels through all three components from definition to runtime dispatch:


Key Stages:

  1. Definition: Application calls Router::get(), Router::post(), etc. via static magic method
  2. Delegation: Router delegates to RouteCollector for the current server via __call
  3. Parsing: RouteCollector uses FastRoute parser to transform route patterns into route data
  4. Storage: Route data stored in FastRoute DataGenerator, named routes stored in array, middleware registered
  5. Initialization: DispatcherFactory lazily loads all route files on first request
  6. Compilation: Route data compiled into FastRoute dispatcher for efficient matching

Sources: src/Router.php49-54 src/Router.php183-188 src/RouteCollector.php53-81 src/DispatcherFactory.php22-50


Integration with Framework Components

The three core components integrate with several Hyperf and external systems:

Dependency Injection Integration

All three components are registered with the Hyperf DI container via ConfigProvider:

ComponentBinding TypeContainer Key
DispatcherFactorySingleton (replaces Hyperf's)Hyperf\HttpServer\Router\DispatcherFactory::class
RouteCollectorFactory (per-server)Created by DispatcherFactory::getRouter()
RouterSingletonHypervel\Router\Router::class

The Router constructor (src/Router.php38-40) receives DispatcherFactory via dependency injection, establishing the connection between components.

FastRoute Integration

The RouteCollector integrates with nikic/fast-route:

Middleware System Integration

Routes register middleware through two managers:

See Middleware Configuration and Exclusions for details on how middleware is processed.

Server Name Routing

The system supports multiple HTTP servers via the serverName property:


The Router::addServer() method (src/Router.php42-47) temporarily changes the server name during route definition, allowing routes to be registered to different servers (e.g., HTTP vs WebSocket).

Sources: src/Router.php22 src/Router.php38-54 src/RouteCollector.php58-79 src/DispatcherFactory.php39-50


Parameter Binding System

The Router class maintains two types of parameter bindings used by the SubstituteBindings middleware:

Binding Storage


Binding Registration Methods

MethodSignaturePurposeLine Reference
model()model(string $param, string $modelClass): voidRegister a model class for a parametersrc/Router.php104-115
bind()bind(string $param, Closure $callback): voidRegister a closure for custom resolutionsrc/Router.php120-123
getModelBinding()getModelBinding(string $param): ?stringRetrieve model binding for parametersrc/Router.php128-131
getExplicitBinding()getExplicitBinding(string $param): ?ClosureRetrieve explicit binding for parametersrc/Router.php136-139

The bindings are consulted by the SubstituteBindings middleware during request processing. See Route Parameter Binding and Model Binding and UrlRoutable for how these bindings are resolved.

Sources: src/Router.php29 src/Router.php36 src/Router.php104-139


Route File Loading

The DispatcherFactory supports loading routes from files via integration with RouteFileCollector:


The initialization process:

  1. First Request: getRouter() (src/DispatcherFactory.php39) checks initialized flag
  2. Load Files: initRoutes() (src/DispatcherFactory.php22) fetches files from RouteFileCollector
  3. Require Files: Each file is required (src/DispatcherFactory.php33-36), executing route definitions
  4. Mark Complete: initialized flag set to true (src/DispatcherFactory.php24)

The Router::group() method (src/Router.php59-68) can also accept a file path directly:


This delegates to registerRouteFile() (src/Router.php83-90), which returns a closure that requires the file.

Sources: src/DispatcherFactory.php22-37 src/Router.php59-90


Current Route Access

The Router provides methods to access information about the currently dispatched route:

MethodReturn TypeDescriptionLine Reference
current()?DispatchedRouteGet current route instancesrc/Router.php144-149
getCurrentRoute()?DispatchedRouteAlias for current()src/Router.php154-157
currentRouteName()?stringGet name of current routesrc/Router.php162-169
input()mixedGet route parameter valuesrc/Router.php174-181

These methods retrieve the route from the current request context via ApplicationContext::getContainer()->get(Request::class)->route() (src/Router.php146-148).

Sources: src/Router.php144-181