2

How i do to get resource based on filter parameter :

As Example : .../api/book?author=X

If an complete example i will be happy !

1 Answer 1

6

Based on FOSRestBundle, you can use QueryParam annotation and ParamFetcher as follow,

use FOS\RestBundle\Controller\FOSRestController;
// ...
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\QueryParam;

class BookController extends FOSRestController
{
    /**
     *
     * @QueryParam(name="author", description="the author of the book")
     */
    public function getBookAction(ParamFetcher $paramFetcher)
    {
         $author = $paramFetcher->get('author'); // Can then be used to filter books on author

         // do something ...
    }

requirements,

  • You've to enable the param fetcher listener by adding param_fetcher_listener: true in the bundle's configuration in your app/config/config.yml file.
Sign up to request clarification or add additional context in comments.

6 Comments

i m looking to use it with handler.. it's right ? public function getActualitesAction(Request $request, ParamFetcherInterface $paramFetcher) { $offset = $paramFetcher->get('offset'); $offset = null == $offset ? 0 : $offset; $limit = $paramFetcher->get('limit'); $ville = $paramFetcher->get('ville'); return $this->container->get('genius_profile.actualite.handler')->all($limit, $offset,$ville); }
handler part : public function all($limit = 5, $offset = 0,$ville) { return $this->repository->findBy(array(), null, $limit, $offset,$ville); }
Yep, the idea looks good as it helps you keep your controllers short and readable. You may also consider using the requirements and the default options of the QueryParam annotation.
i haved tryed my method with public function all($limit = 5, $offset = 0,$ext) { return $this->repository->findBy(array(), null, $limit, $offset,$ext); } but did not work :/
Is there no out-of-the-box solution to automatically create filters for all entity fields?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.