Routing is the heart of the request-to-controller mapping system in Apivalk. It is designed for high performance, ease of use, and zero-configuration through automated discovery.
The Router is initialized with a ClassLocator and a CacheInterface implementation.
During initialization, the Router uses RouteCacheFactory to ensure the route cache is built and up-to-date.
During dispatch, the Router retrieves a route index from the cache.
The Router iterates through the index, comparing the current request’s URI against the pre-generated regex patterns for each route.
If a match is found, the Router retrieves the full route definition from the cache, identifies the associated controller and request classes.
The Router then populates the request object and executes the middleware stack.
By combining documentation-driven route definitions with automated discovery and caching, Apivalk provides a powerful routing system that requires minimal manual intervention.