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

stefanwegner's avatar

Dynamic routes.php from Eloquent

Hi there,

i was tinkering with an idea of storing all routes in my database, that usually are stored in the routes.php file.

The reason behind it, was to be able to connect routes and permissions directly via an eloquent relation + storing route translations as well ... (and be able to manage these in my applications backend)

I have a table called "Routes" and another table "Route_Translations".

Routes: id, location['frontend','backend', 'api'], alias, method, action, middleware

Route_Translations: id, route_id, language_id, url

Each Route can have many translations for all available languages.

My problem is not the implementation ...

Is there a downside to store all routes in my db?

I was thinking about security issues ... If i store all my routes in a db and someone could access my db, he/she will be able to cause a lot of trouble simply by altering the controller action or delete the table contents

(I will make use of route-caching later on - so performance shouldn't be a problem.)

Thanks for your ideas ...

0 likes
13 replies
bashy's avatar

A CMS would have dynamic routes for the frontend. Is that what you mean?

stefanwegner's avatar

Wow, that was a fast reply ;-)

I'm not talking about mywebsite.com/posts/{slug} ... I was thinking to store ALL routes - frontend and backend in a db and dynamically generate them in my routes.php file ...

Meaning, i will store all route informations, like method, url, alias, uses, middleware in a db row and fetch these into the native laravel router, when loading the routes.php file.

And what my biggest concern is - if someone could manipulate and destroy my whole app endpoint structure .. I hope that was understandable ...

bashy's avatar

Okay so everything in DB. The only thing I'd say is (depending on the app's nature) is that, things will get messy, fast. How will you check for duplicate pages or "slug segments"? Is it all being added by a user?

ARCANEDEV's avatar

I think this is a bad approach to store all the routes in you DB.

And i don't know how you're going to manage that with your controllers, views, forms, validations, model bindings ...

How about the performances ? Are you planning to cache all your routes from you DB ?

bashy's avatar

@ARCANEDEV

(I will make use of route-caching later on - so performance shouldn't be a problem.)

stefanwegner's avatar

@bashy - why messy? Sure, it's a large application - and each section of the app - e.g. shop, blog, forum, account will have it's own prefix (or route-group so to speak - my route alias structure is something like de::frontend@shop.products.show - to avoid trouble with duplification) - and sure you have to pay attention of the order and unique slugs/params/format etc. but you need to be aware of it anyways in your classic routes.php file ... The classic "administrator" will be the only one, accessing or editing these, since it's a backbone of the application - BUT - there will be a table for Redirects as well - and again - I plan to make use of the Eloquent Relation Model ...

@ARCANEDEV - everything else is filebased and done the classic way, it's just the routes that are stored in the db. I see absolutely no downside in performance since i will make use of route:cache and the routes will only be loaded once.

jimmck's avatar

I think you should store app related code in the database as well! Besides the cool I stuck it in the database factor, what does this get you besides another layer? The routes file is a php file. Most routes are almost fully coded except for parameters. And when the database is down you are down. Not only is the web server serving code involved, now the database server is involved.

ienderli's avatar

I was thinking about a similar idea, maybe not too similar we'll see...

I was thinking of some sort of factory that would read all the actual, full uri, routes in an application and then put them in redis/memcached. While storing them in the cache you could associate data you might want. so instead of having to look up slugs in the database when a uri is requested, a full route would be cached. but maybe you query redis with some data you have in mysql just enough to get the functionality your looking for but still use laravels routing?

Thoughts?

stefanwegner's avatar

Ah, okay, didn't thought of the fact when the database is not available ... that would suck for sure @jimmck

The benefit I'll get: I have 10 different languages in my app and maybe 200 single routes need to be translated ... manually i would have to write ...

Route::get('de/shop/produkte/{productSlug}' ...

Route::get('en/shop/products/{productSlug}' ...

Route::get('fr/shop/produits/{productSlug}' ...

Route::get('pl/shop/produkty/{productSlug}' ...

and so on ...

with dynamic db-based routes (with translations) i can render these in a foreach loop and have also the ability to connect them to a tracking component or an activity log or reports or or or ....

bashy's avatar

The layout wouldn't be messy I guess. Just the thought of it all being in the database I guess :P

stefanwegner's avatar
stefanwegner
OP
Best Answer
Level 7

hm, I think I will take an hybrid solution ... Store all routes in my relational database - BUT - generate a physical routes.php file each time an update event occured and recache the routes when done :-)

Please or to participate in this conversation.