Skip to main content

Dispatch Process

When the dispatch() method is called, the router performs the following steps:
  1. Extract Request Information: It captures the HTTP method (from $_SERVER['REQUEST_METHOD']) and the URL path (from $_SERVER['REQUEST_URI']).
  2. Find Matching Routes: It queries its associated RouterCacheInterface to find all route definitions that match the current URL path using pre-compiled regular expressions.
  3. Validate Method:
    • If no routes match the path at all, a NotFoundApivalkResponse (404) is returned.
    • If a path matches but the requested HTTP method is not supported for that path, a MethodNotAllowedApivalkResponse (405) is returned.
  4. Prepare Request and Controller:
    • It identifies the AbstractApivalkController class associated with the matching route.
    • It identifies the corresponding ApivalkRequestInterface class.
    • It instantiates both, populating the request object with data from the route (including extracted path parameters).
  5. Execute Middleware Pipeline: It hands off the request and controller instance to the MiddlewareStack for processing and final execution.

AbstractRouter

The AbstractRouter provides the base functionality and dependency management for the router. It handles:
  • Controller Factory: Manages the ApivalkControllerFactoryInterface used to instantiate controllers.
  • Router Cache: Manages the RouterCacheInterface used to retrieve route definitions.
  • Default Implementations: Provides a static createDefault() method for quick bootstrapping with standard components.

Initialization

The router is typically initialized during the bootstrapping phase of the application:
use apivalk\apivalk\Router\Router;
use apivalk\apivalk\Router\Cache\RouterFilesystemCache;
use apivalk\apivalk\Util\ClassLocator;

// Set up the cache and locator
$locator = new ClassLocator(__DIR__ . '/Controllers', 'App\\Controllers');
$cache = new RouterFilesystemCache(__DIR__ . '/cache', $locator);

// Create the router
$router = new Router($cache);
By separating the matching logic from the route discovery (handled by the cache), the Router remains fast and focused on its single responsibility: dispatching requests.