Skip to content

Conversation

@binaryfire
Copy link
Contributor

@binaryfire binaryfire commented Dec 21, 2025

Problem

Routes registered via loadRoutesFrom() in service provider boot() methods are not loaded.

  class MyServiceProvider extends ServiceProvider
  {
      public function boot(): void
      {
          $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');  // ❌ Never loaded
      }
  }

Cause

DispatcherFactory captures the route file list in its constructor:

  public function __construct(protected ContainerInterface $container)
  {
      $this->routes = $container->get(RouteFileCollector::class)
          ->getRouteFiles();  // ← Captured here
      // ...
  }

But loadRoutesFrom() adds files to RouteFileCollector during service provider boot(), which happens after the DispatcherFactory may have been constructed. The captured list is stale, so late-added routes are never loaded.

It looks like lazy loading via initRoutes() was intended to defer route loading, but capturing the route file list in the constructor defeats this purpose.

Solution

Move route file fetching from the constructor to initRoutes():

  public function __construct(protected ContainerInterface $container)
  {
      $this->initAnnotationRoute(AnnotationCollector::list());
      // Don't capture routes here
  }

  public function initRoutes(): void
  {
      // Fetch at initialization time, after service providers have booted
      $routes = $this->container->get(RouteFileCollector::class)->getRouteFiles();

      foreach ($routes as $route) {
          if (file_exists($route)) {
              require $route;
          }
      }
  }

This ensures route files are fetched when getRouter() is first called, after all service providers have had a chance to register
their routes.

Performance

No impact. Route initialization happens once per worker process at startup. The fix adds one container lookup that executes once during worker startup.

Changes

  • DispatcherFactory: Move route file fetching from constructor to initRoutes()
  • Added test verifying routes added after construction are loaded

… construction

Route files were captured in the DispatcherFactory constructor, but
loadRoutesFrom() adds files during service provider boot() which happens
after construction. Routes added via loadRoutesFrom() were never loaded.

Move route file fetching from constructor to initRoutes() so routes are
fetched at initialization time when getRouter() is first called, after
all service providers have booted.
@albertcht albertcht added the bug Something isn't working label Dec 21, 2025
@albertcht albertcht merged commit 5e37d04 into hypervel:main Dec 21, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants