V
V
Vasyl Fomin2018-08-13 17:03:42
Laravel
Vasyl Fomin, 2018-08-13 17:03:42

How to build routes in Laravel with the same template for different entities?

There is a site on Laravel
The site has routes for ads:

Route::get('{post}', '[email protected]);   //например: http://my-domai.com/slug-some-post-1

You also need to add similar routes, only for cities:
Route::get('{city}', '[email protected]);    //например: http://my-domai.com/slug-some-city-1

Both ads and cities are added and stored in the database.
These paths should not have any prefixes (such as post, city, ...)
So far, only the idea of ​​​​implementing one route comes to mind: and resp. controller method, where there will be a check for the presence of slugs in the database for different models (cities, ads) Are there any other options or suggestions? Perhaps this can be solved somehow if you store the slugs or even the URL in the database in one table (for example, as in the Drupal CMS)?
Route::get('{entity}', '[email protected]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-08-13
@Kostik_1993

No, if you don’t add any impurities to the slug, then just take out the table with the slugs separately, as you say, done in Drupal. And use polymorphic relationships https://laravel.com/docs/5.6/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question