FOSRestBundle can't get param fetcher
-
11-12-2019 - |
Question
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.
Solution
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
OTHER TIPS
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.