VOOZH about

URL: https://deepwiki.com/hypervel/router/2.1-router-class

⇱ Router Class | hypervel/router | DeepWiki


Loading...
Menu

Router Class

Purpose and Scope

The Router class serves as the primary interface for defining routes and configuring parameter bindings in the hypervel/router package. It provides both instance-based and static methods for route registration while delegating most route definition operations to RouteCollector. The class manages two types of parameter bindings—model bindings and explicit bindings—that are used by the SubstituteBindings middleware during request processing.

For information about how routes are collected and compiled, see Route Collection and Registration. For details on parameter resolution during requests, see Route Parameter Binding and Model Binding and UrlRoutable.

Sources: src/Router.php1-189


Class Architecture

The Router class acts as a facade that combines route definition capabilities (delegated to RouteCollector) with parameter binding configuration. It maintains binding registries and provides access to the currently dispatched route.


Sources: src/Router.php20-40 src/Router.php49-54 src/Router.php183-189


Route Definition Methods

The Router class delegates all standard route definition methods to RouteCollector through the __call magic method. This allows the Router to expose the full RouteCollector API while maintaining its own additional functionality.

Delegation Pattern

Method Call on RouterActual Execution
$router->get($uri, $action)RouteCollector::get() via __call
$router->post($uri, $action)RouteCollector::post() via __call
$router->addRoute($methods, $uri, $action)RouteCollector::addRoute() via __call
Any other RouteCollector methodDelegated via __call

The __call implementation retrieves the appropriate RouteCollector for the current server and forwards the method call:


Sources: src/Router.php49-54 src/Router.php95-99


Route Grouping

The Router class provides specialized handling for route groups, including support for loading routes from files.

Group Registration

The group() and addGroup() methods allow organizing routes with shared prefixes and options:

ParameterTypeDescription
$prefixstringURI prefix for all routes in the group
$sourcecallable|stringClosure with route definitions or path to route file
$optionsarrayShared options (middleware, namespace, etc.)

When $source is a string, the Router treats it as a file path and wraps it in a closure that requires the file:


The file registration includes existence validation and returns a closure that requires the file at execution time:

Sources: src/Router.php59-90


Parameter Binding System

The Router class maintains two registries for parameter binding configuration. These bindings are queried by the SubstituteBindings middleware during request processing to resolve route parameters into rich objects.

Binding Types


Model Bindings

The model() method registers automatic database model resolution for a route parameter:

ValidationPurpose
class_exists($modelClass)Ensures the class is autoloadable
is_subclass_of($modelClass, Model::class)Ensures it's a Hyperf Model

When registered, the SubstituteBindings middleware will automatically query the database using the parameter value to retrieve a model instance.


Sources: src/Router.php104-115 src/Router.php128-131

Explicit Bindings

The bind() method registers a custom closure for parameter resolution, providing complete control over how the parameter is resolved:


The closure receives the parameter value and returns the resolved object. Explicit bindings take precedence over model bindings in the resolution order.

Sources: src/Router.php120-123 src/Router.php136-139


Current Route Access

The Router class provides methods to access information about the currently dispatched route during request processing.

Current Route Methods

MethodReturn TypeDescription
current()?DispatchedRouteReturns the current route instance
getCurrentRoute()?DispatchedRouteAlias for current()
currentRouteName()?stringReturns the current route's name
input($key, $default)mixedReturns a route parameter value

These methods retrieve the current route from the Hyperf request instance via ApplicationContext:


Sources: src/Router.php144-181


Static Access Pattern

The Router class implements the __callStatic magic method to enable static method calls that resolve to instance methods on the container-managed Router instance.

Static Resolution Flow


This pattern allows both static and instance usage:


Sources: src/Router.php183-189


Server Management

The Router class supports multi-server configurations through the addServer() method, allowing different route definitions for different HTTP servers.

Server Context


The server context is maintained during callback execution and automatically restored:

StateValue
InitialserverName = 'http'
During addServer('ws', ...)serverName = 'ws'
After callback completesserverName = 'http'

Sources: src/Router.php22 src/Router.php42-47


Integration with Core Components

The Router class integrates with multiple core systems in the routing pipeline.


Key Integration Points

ComponentRelationshipPurpose
DispatcherFactoryDependencyProvides access to RouteCollector instances
RouteCollectorDelegation targetReceives all route definition calls
SubstituteBindingsConsumerQueries binding registries during request processing
ApplicationContextService locatorEnables static access and current route retrieval
DispatchedRouteReturn typeRepresents the currently executing route

Sources: src/Router.php38-40 src/Router.php49-54 src/Router.php128-139 src/Router.php144-149