Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ldommer's avatar
Level 21

Logic in route file / Best practice

Laravel best practice is not putting logic into a route file.

This is pretty obvious and right.

But does this only refer to not using the SRP and to not delegating the request to properly build classes (controllers, services, fat models and so on)?

Or does this also exclude some kind of queries and loops inside your route file in order to write less code?

For example in a CMS or admin backend scenario where you have to generate a lot of similar (ressource) routes for CRUD models which names you can get from a config file or database query.

Some pseudo code for explanation:

// routes/web.php
foreach(getModelNames() as $name) {
    Route::resource(calculateRouteName($name), calculateControllerName($name));
}

So is this a good idea as route files can be cached or a bad one?

0 likes
1 reply
sr57's avatar

Good idea, it's code but not really logic ...

Laravel, MCV framework, guides us to put logic in a standard place just to have code esay to maintain, your solution does not break the standard logic.

Beyond this code, Laravel keeps routes in a array, and this array will will be the same if you code without the foreeach.

Please or to participate in this conversation.