-1

I got a lot of products, and it can be filter with a lot of different parameters. So user input search parameter in a form, and then the list is filter by those parameters.

I have try to create a route like this :

/**
 * Display a list of product
 * @Route("/product/list/{name}/{price_min}/{price_max}/{publish_date}/{supplier_code}", name="product_list")
 */
public function listProduct(){

// ... Check  parameters format and then escape special caracters
// ... Display product logic
return $this->render('Product/product_list.html.twig', $array_params_view);
}

I know you can provide optional parameter, but this solution looks really bad to me... I think there might be another solution.

I have think to use the Request instead of a lot of parameter, but if I do so, I loose the fonctionality of nice and easily readable URL, and maybe it'll be more difficult to manage routing.

I don't know what is the best solution for search functionnality.

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
vincent PHILIPPE
  • 975
  • 11
  • 26
  • 2
    You almost certainly just want to use a good old fashion query string for this. What 'functionality' do you really get by having nice and easily readable URLs with a gazillion placeholders. Ever notice how many other sites have long and complicated URLs? Did it ever really matter to you as an user? – Cerad Sep 07 '20 at 11:52

2 Answers2

3

If you use route for search into your list, I think you need to read this : link

Query String is a better way for search.

// the query string is '?foo=bar'

$request->query->get('foo');
// returns 'bar'
Gary Houbre
  • 857
  • 8
  • 24
1
 /**
 * Display a list of product
 *
 * @Route("/list/", name="product_list")
 *
 * @param Request     $request
 *
 */
public function listProduct(Request $request)
{

    $name          = $request->query->get('name');
    $price_min     = $request->query->get('price_min');
    $price_max     = $request->query->get('price_max');
    $publish_date  = $request->query->get('publish_date');
    $supplier_code = $request->query->get('supplier_code');

    $list_products = $this->getListProducts($name,$price_min,$price_max,$publish_date,$supplier_code);
    
    //Next code
    ......
}

You would only have to control within the getListProducts function or whatever you call it, that the arguments can arrive as null

sefhi
  • 70
  • 1
  • 6