I have an application which I am trying to build using Slim PHP and at the moment I am struggling with one Route I am trying to sort out. Basically what happens is I go to GET /:slug and this provides me with a product view. However if there are variations on this product, it will be an intro page in which a visitor can target an exact product. The differences in these products will be :
Possibly a few others. The problem I am having is that I am not 100% sure which if any there will be. Do I write my routes for every possibility? Or is there a way I can get this to take a more relaxed approach to how this is formed?
I saw an example where someone did the following :
$app->get("/:slug(/:dial(/:strap(/:material)))", "Route\To\Controller:Action");
However I am assuming this will require this to be in that set order?
Also a note to add would be that some products have no additional filters and respond directly to /:slug whereas others may require all of the additional filters.
These results are all set in the database. I grab all products that have a similar slug as it equates to the product name, then each filter or collection of filters will be determined by certain parts in the database. For example: /this-product/this-dial/this-color/this-strap/this-material all of these values will be stored within the database table for the product - they either have a corresponding value for material - or it is NULL.
This allows me to on the root URL with the SLUG I can show variations - which will aid in the shopping experience in my opinion.
Does anybody have a solution?? Anyway I could achieve this? Am I going about it the wrong way?
One possible way is to render different templates based on the number of returned products. For example, if you are using Twig Template extension you can easily iterate over the set of results and render two templates one for Product Description and one for Product Group.. Alternatively, You can handle this while rendering a view in your SLIM Route
$app->get('/:slug', function ($slug) use ($app) {
$db = new dbConn();
$Products = $db->ProductQuery(); // DB querying for 'slug' & returned product (s)
if( // here check how many $Products and if = 1 ) {
$app->render('single_product_view.html',$Products);
} else if( /// > 1 ) {
$app->render('multiple_product_view.html',$Products);
} else {
$app->response->redirect($app->urlFor('notFoudnt'), 404); // or whatever other route..
}
})->name("product");
As for the querying why do you want to have a route for each product attribute ?
Can't you in this case have one route for the product view and add a query parameter ( attributes ) to URL
/this-product?dial=this-dial&color=this-color&strap=this-strap&material=this-material
$app->get('/:slug', function ($slug) use ($app) {
$Dial = $app->request()->get('dial');
$Color = $app->request()->get('color');
$Strap = $app->request()->get('strap');
$Mat = $app->request()->get('material');
// same as above, build your query string and render corresponding view
})->name("product");
You can also Group your routes ( for ajax request maybe.. )
$app->group('/api', function () use ($app) {
// Products attributes group route
$app->group('/products', function () use ($app) {
// Get Products with color = :color /api/products/color/:color
$app->get('/color/:color', function ($color ) {
});
// Get Products with Strap = :strap /api/products/strap/:strap
$app->get('/strap/:strap', function ($strap) {
});
// and so on..
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With