Declaring/defining Routes

I’ve not long picked up Slim and I love it, but there’s one thing (so far!) I don’t like, which is the need to specify routes in some central place, index.php, using something like

$app->get('/myroute', [SomeController::class, 'methodName']);

Is there a way of achieving the same thing, constructed differently? This way spreads information about SomeController class and all its relatives into a central place, and requires manual updating when I want to add a new route.

What I’d like to do is to register the routes a controller supports from the controller or, maybe more preferably, from a class associated with each controller and the route methods are either discovered by that class or the controllers register their route-methods with .

The problem I’ve encountered is that it needs to happen before calling $app->run() or at least before receiving a request. Has anyone tried using Doctrine Annotations to do this? I’m open to other ways, obviously, it was just an idea that came to me late last night (and so might be completely bonkers, especially since I’ve never used them before).

One way to achieve that:


return static function (Application $app) {
 // add some routes


(require __DIR__.'/routes1.php')($app);
// (require __DIR__.'/routes2.php')($app);

Well, yes, but that’s still manually declaring them, and having to keep the declarations in sync with the implementation. I don’t want to do work that the computer could do.

I’m working on a (CakePHP) project that works like this, and I can tell that it’s lets say “sub-optimal” in the long run because, the bigger the project, the more you don’t know what routes are used and unused. It’s a nightmare from a maintaining perspective. If you use single action controllers than you know exactly the routes and you can “jump” in your IDE from the route directly to the Action class. Also refactoring is much easier. That’s why I would always prefer “explicit” routes, like in Slim. Also handling special kinds of requests, like CORS preflight requests is much better to handle with “real” routes.

the bigger the project, the more you don’t know what routes are used and unused

Surely that’s even worse if you’re doing things manually? Whoever is responsible for removing a route has to remember to remove the implementation and the explicit entry for it. If the declaration of the routes were generated that wouldn’t happen.

Regarding IDEs, well, I don’t use one :slight_smile: