i would like to build a route that catch clean seo friendly url and call correct controller to display page. Examples:
https://mypage.com/some-friendly-url-separated-with-dashes [PageController]
https://mypage.com/some-cool-eletronic-ipod [ProductController]
https://mypage.com/some-furniture-drawers [CategoryController]
So I have in app route:
Route::get('/{friendlyUrl}', 'RouteController@index');
Each friendly url is a unique url(string) so there is no duplicate between pages/products/categories. There is also no pattern between urls – they could be any string used in seo(only text plus dashes/ sometimes params).
Is it wise to build one db table that keeps all urls in on place with info what to call ( url | controller_name | action_name) – as an example.
Another question is – how to call different controllers depending on url used? (for above example -> RouteController catch friendly urls -finds match in db table -> then calls correct controller)
Many thanks for any help.
Have a nice day
Mark
3
Answers
It’d be better if you just use a prefix for each type like this:
Then for achieving this, create three routes like this
These URLs would be SEO friendly
You need create a table call slugs.
Then create a unique slug (can be auto generated or specified) for each page, product, category.
slug records also have columns to get Controller and params, ex:
type
andid
There’s two approaches you can take to this.
Proactive:
In
web.php
Then you can determine the product in the appropriate controler via e.g.
The downside to this approach is that all routes are registered on every request even if they are not used meaning you hit the database on every request to populate them. Also, routes can’t be cached when using this approach.
Reactive:
In this approach you use the fallback route:
In
web.php
: