(06-29-2022, 09:55 AM)iRedds Wrote: In the index() method, check for the presence of values. And there you can already call the method or not call it.
Routes only work with a path.
Well, routes are working with path, but you can still need to use querystrings (typically in a search/filter context).
Finally, I made it with 2 different routes :
App/Config/Routes.php
PHP Code:
$routes->get('api/products/search/', 'Api\Products::search');
$routes->resource('api/products', ['controller' => 'Api\Products']);
I can then define a proper search() method within my controller without having to pass thru index()
/App/Controllers/Api/Products.php
PHP Code:
class Products extends ResourceController
{
public function search()
{
$arrFilters = $this->request->getVar();
$res = serializer_instance()->toArray(repo_instance()->getProductRepo()->findBy($arrFilters));
if (!$res) return $this->failNotFound();
return $this->respond($res);
}
}
Note that the search can take several parameters thru the getVar() array so that following call is working /public/api/products/search/?maxprice=10&category=beverages&subcategory=beer