Something like this might work for you.
Route::get('/pages/{page}', 'PagesController@show');
$page = Page::where('page', $page)->get();
return view('pages.show', compact('page'));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am creating something like very simple personal CMS. I have the admin section, hardcoded in the routes/web.php file. That's no problem, everything goes fine (except 404 page, which I'm working on at the moment). But I have a problem with basic front-end routes. The system is like this: The admin will create a new subpage, it will name it and assign a module (module could be for example articles, contact-form, gallery, static page, etc.). This will be stored to a database's table. To this point everything is OK for me. The problem is, I need to select the data from the database's table and create the routes from them. And this is problem. Is OK to use DB::class in routes/web.php? I think not, because of MVC. The main reason, why the subpage's name can't be hardcoded in a web.php file is, that I have no chance to know, how the admin will name the subpage. For example, the article module could be named as "Articles", or "News", or "Blog", so I can't write something like Route::get('News', 'ArticleController@index'); :-/
I thought about two ways:
Use DB::class directly in routes/web.php file. Something like this: $dynamicRoutes = DB::table('structure')->select()->join('modules', 'structure.module_id', '=', 'modules.module_id')->get(); //var_dump($dynamicRoutes);exit; foreach($dynamicRoutes as $route) { eval "Route::get('/".$route->structure_url."/', '".ucfirst($route->module_url)."Controller@index');"; } The problem is, that I always received the syntax error, I really don't know why (something wrong here: Route::get('/"' (T_CONSTANT_ENCAPSED_STRING), expecting '(')
Define everything hardcoded routes (so routes for admin section) and each other url, which will be not defined, will point to "RouteController@process", there I am able to process the URL. But, there's a problem. If I set something like Route::get('/{slug}', 'RouteController@process') it's working very fine for www.sitename.com/example. But not worked for www.sitename.com/example/2. How can I secure, that everything else, as defined will be point to RouteController@process?
Thank you for a help
Please or to participate in this conversation.