Key Responsibilities
- Request Matching: Analyzing the incoming HTTP method and URI to find a matching route definition.
- Route Discovery: Automatically scanning specified namespaces for controllers and extracting their route definitions.
- Path Parameter Extraction: Using regex patterns to identify and capture dynamic parts of the URL (e.g.,
/user/{id}). - Performance Optimization: Caching the discovered routes to disk to ensure that heavy scanning logic only runs when necessary.
Core Components
Router: The main dispatcher that handles the matching logic and produces a response.Route: A data object representing a single API endpoint, including its URL, HTTP method, and metadata.- Rate Limit: A subsystem for protecting endpoints from excessive requests.
- Cache System: A subsystem responsible for persisting and retrieving route definitions for maximum efficiency. Learn more
How it Works
- The
Routeris initialized with aClassLocatorand aCacheInterfaceimplementation. - During initialization, the
RouterusesRouteCacheFactoryto ensure the route cache is built and up-to-date. - During dispatch, the
Routerretrieves a route index from the cache. - The
Routeriterates through the index, comparing the current request’s URI against the pre-generated regex patterns for each route. - If a match is found, the
Routerretrieves the full route definition from the cache, identifies the associated controller and request classes. - The
Routerthen populates the request object and executes the middleware stack.