The simplified scenario is the following:
I have a few pages in the database (ideally not more than 10-20, but there is no hard limit). They all have some content and a "slug" (there are slugs that contain a forward slash, if that matters). I'm trying to register route(s) to these slugs to a PageController
that displays the content of the given page. Here are my ideas about the approach:
- Register a single "catch 'em all" route (which should be the very last registered route). This is definitely bad.
- Query all links from the database and register individual routes to them. This is easy, but I have to query the DB (which might not be optimal) and register tens, potentially hundreds of routes.
- What I also tried: register a HTTP Middleware, get the query parameter by using
$request->path()
, check if this is an existing slug, and if so, pass it to thePageController
. I achieve this by simply callingRoute::get($request->path(), 'PageController@show')
.
Both of the last two methods seem to work, but there is a question: how do I tell the Controller which page to dislay?
Or is there a better approach?
Thanks in advance
Edit: I definitely don't want to use a 'prefix' and add the slug as a parameter to it. The defined slug should be the exact URL to the page.
All approaches you've listed can be a valid approaches to solving this problem (even the first one if you attach a
where
condition on the route). However in my opinion the best approach, performance wise, would be based on the second solution, and would be to generate and write the routes to a file when needed and then caching them. So here's how I'd go about doing it:1. Create a separate routes file where you'll be storing your page routes, let's say in
app/Http/Routes/page.php
. This is where you'll be writing the route definitions for your pages. You'll also need to add this to themap
method of theApp\Providers\RouteServiceProvider
class:2. Then you need to generate and write the route definitions for the pages to that file. Something like this should suffice:
The code above should be executed on specific events that you can attach to the
Page
model:created
,deleted
,updated
(but only if the slug was modified during the update).3. To access the page details in your controller you just need to use the
path
of the request, since that's your slug. So this would do:There you go, now you have route definitions for all your pages. And the fact that they're cached mitigates any performance penalties you would get when there are lots of routes and you're only touching the database when you are making changes to those routes, not on every request.