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

MHertel's avatar

Best way to handle routes from Database

Hello everyone,

I'm building a project in which i want to store routes into the DB and let the user create all the urls, and I'd like to find the best way to handle the logic I'm using Laravel 5.1 (the current dev master).

At the moment I've got simple DB structure for testing

  • table: menu
  • id: increments
  • name: string
  • parent: integer, default 0
  • content: text
  • type: string
  • timestamps

I've got a catch-all route at the end of my routes.php(is only working with GET requests ATM)

`Route::get('{path?}', "ModuleRouter@index")->where('path', '.+');`

this routes will catch anything and I'll be able to work with a structure like

`page-1/level-2/level-3/level-4`

I thought of different way to handle this, but in the end, I want to display the menu somewhere and create the navigation, so eventually from the table menu at some point I'll need a Menu::all() and I'll build the navigation with that object.

So far I've created a controller called MenuRouter, the index method is getting as parameter the url structure, then with a recursive function I'm looping through the Menu::all() and while the function is running I'm matching the url structure and in the end I'm getting the printed menu and the requested page of false in case the page has no match so I can throw a 404, this code(I can post it if needed, I just don't want make this post massive) is working but I'm not sure is the right way to tackle the problem so I've got a question:

Is this the right way to do it and does the logic make sense?if not what is the right way to do it?

Thanks for helping out

0 likes
0 replies

Please or to participate in this conversation.