Search code examples
phpmezzio

Split route configuration in zend-expressive


I'm using zend-expressive (3) to build my REST APIs. The file config/routes.php contains my routes, e.g.

return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
    $app->get('/', Start\HomePageHandler::class, 'home');
    $app->get('/api/ping', Start\PingHandler::class, 'api.ping');
};

Every new module adds a lot of new routes. This gets confusing. Is there a way to split the route configuration into multiple files?


Solution

  • There are 2 ways to add routes. The first one is by how you do it, injecting the routes. You can use a custom delegator factories to split up routes into modules.

    The second is by configuration. You could created a bunch of files in a config/routes/ dir and autoload those. However if you prefer the routes within your modules, I suggest to stick with the first method.

    Just be aware that using both methods at the same time might cause unwanted side effects so you should choose 1 method.