Reputation: 500
I'm creating a Laravel 5.1 application that lists products on pages. Now each product on the database has the url of the product detail page. I have done a method that exports all those urls and convert's them to laravel routes, and the routes are written into a file and included on the laravel routing. I have done that on that way in order to be able to optimize the routing using laravel's routes:cache command. Now my question in fact is about optimization, which way would be better, to have a file with all routes, let's say 100K routes or to have a single entrance point that compares the route in question inside the database and return the respective product.
Upvotes: 0
Views: 1332
Reputation: 667
the fact that you use a framework you will be enforced to optimize your code
route file allow you to get the fastest way to minimize the number of mapped URLs.
the notion of route offered by Symfony to laravel allow you to manipulate all variables of your url with one single route , for example :
route::get('{category}/{id}/{slug}/{var1}/{var2}/{var3}/{varX}','yourController@yourMethod');
this route will send all variables in the url to yourMethod();
Upvotes: 0
Reputation: 2107
There isn't a need to have an individual route for each product. Possibly Store a unique slug(Semantic URL) in the database? Then have one route that displays a product based on a what is passed to the route.
user friendly slug could be something like 't-shirt-9000'. If you don't want this, you can always use the unique id you already have set for the product in the database.
Within your show method, you would need to query the DB with the slug past in the request to the database.
quick example:
In routes.php
Route::get('/products/{product}', 'ProductController@show');
Product Controller
public function show($product)
{
$productRequested = Product::where('url_slug', $product)->first();
// Do whatever from here
}
Upvotes: 4