Welcome Guest, Not a member yet? Register   Sign In
RESTFul route + search terms via querystring

(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 :

PHP Code:
$routes->resource('api/products', ['controller' => 'Api\Products']); 

I can then define a proper search() method within my controller without having to pass thru index()

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

Messages In This Thread
RE: RESTFul route + search terms via querystring - by b126 - 06-30-2022, 07:53 AM

Theme © iAndrew 2016 - Forum software by © MyBB