FOSRestBundle can't get param fetcher

10,580

Solution 1

I often have problems with FOSRestBundle and outdated cache. It simply doesn't refresh sometimes if I make a change in annotations. First, try removing contents of your app/cache directory (rm -rf app/cache/*).

I didn't check if parameters are mapped to method arguments by a type or a name. If it's the later than your parameter should be called $paramFetcher (not $params):

public function checkPointsAction(ParamFetcher $paramFetcher) { }

Edit: have you enabled param fetcher listener?

fos_rest:
    param_fetcher_listener: true

Solution 2

I had the same issue, as pointed out in the comment. I solved this by changing the Variable name from

  public function addMapAction(ParamFetcher $pf) {

to

  public function addMapAction(ParamFetcher $paramFetcher) {

And all the sudden i worked like a charm, apparently $paramFetcher is required.

Share:
10,580
Ivan Fateev
Author by

Ivan Fateev

Software Engineer with vast experience in both client-side and server-side development. Currently working with Unity3D/C# developing games in Glu Mobile.

Updated on June 17, 2022

Comments

  • Ivan Fateev
    Ivan Fateev over 1 year

    I'm trying to make my controller work with param fetcher. I did all instructions specified in the documentation. So what I have: config fos_rest.yml:

    fos_rest:
      param_fetcher_listener: 'force'
      view:
          formats:
              json: true
              xml: false
              rss: false
          mime_types:
              json: ['application/json', 'application/x-json', 'application/vnd.example-com.foo+json']
              png: 'image/png'
          failed_validation: HTTP_BAD_REQUEST
          default_engine: twig
          view_response_listener: 'force'
      format_listener:
          default_priorities:
              - json
      routing_loader:
          default_format: json
    

    sensio_framework_extra: view: { annotations: false } router: { annotations: true }

    And my controller

    <?php
    
    namespace Push\PointsBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Nelmio\ApiDocBundle\Annotation\ApiDoc;
    use FOS\RestBundle\View\View;
    use FOS\RestBundle\Controller\Annotations\QueryParam;
    use FOS\RestBundle\Request\ParamFetcherInterface;
    use FOS\RestBundle\Controller\Annotations\RequestParam;
    use FOS\RestBundle\Request\ParamFetcher;
    
    class RestController extends Controller
    {
        /**
         * @QueryParam(name="latitude", requirements="[0-9.]+", default="0", description="")
         * @ApiDoc()
         */
        public function checkPointsAction(ParamFetcher $params)
        {
            $view = new View();
            return $this->get('fos_rest.view_handler')->handle($view);
        }
    }
    

    When I calls method I get:

    Controller "Push\PointsBundle\Controller\RestController::checkPointsAction()" requires that you provide a value for the "$params" argument (because there is no default value or because there is a non optional argument after this one).

    What I did wrong or missed something? Thank you.

  • Ivan Fateev
    Ivan Fateev about 11 years
    Thank you for reply. I tried to clear the cache and tried to rename argument, but it didn't help at all :(
  • Ivan Fateev
    Ivan Fateev about 11 years
    I looked at logs and noticed that symfony notified only BodyListener. There is no entries about PararmFetcherListener
  • Jakub Zalas
    Jakub Zalas about 11 years
    Have you enabled param fetcher listener? I updated my answer with proper config.
  • Ivan Fateev
    Ivan Fateev about 11 years
    Ofcourse, I posted my config too. And I enabled it even with force option and tried with true option too. There is no difference for my case, still got error
  • Ivan Fateev
    Ivan Fateev almost 11 years
    I forgot to include config file in config.yml and yes param should be exact ParamFetcher $paramFetcher
  • cmenning
    cmenning over 9 years
    I was pulling my hair out trying to get QueryParam working... This did it for me.