繁体   English   中英

FOSRestBundle:ParamFetcher 错误

[英]FOSRestBundle : ParamFetcher error

我在我的项目中使用 FOSRestBundle 我已经配置了这个路由来访问不同的数据 kinf :

``

   /**
     * @Rest\Get("")
     *
     * @Rest\QueryParam(
     *     name="categoriesId",
     *     requirements="[0-9a-zA-Z\- \/_:.,\s]",
     *     default="",
     *     description="The categories ids."
     * )
     * @Rest\QueryParam(
     *     name="orderBy",
     *     requirements="[a-zA-Z0-9]",
     *     default="score",
     *     description="The keyword to search for."
     * )
     * @Rest\QueryParam(
     *     name="order",
     *     requirements="asc|desc",
     *     default="desc",
     *     description="Sort order (asc or desc)"
     * )
     * @Rest\QueryParam(
     *     name="limit",
     *     requirements="\d+",
     *     default="-1",
     *     description="Max number of celebrities returned."
     * )
     * @Rest\QueryParam(
     *     name="offset",
     *     requirements="\d+",
     *     default="0",
     *     description="The offset"
     * )
     *
     * @Rest\View(serializerEnableMaxDepthChecks=true)
     * @param ParamFetcherInterface $fetcher
     * @param EntityManagerInterface $em
     * @return array
     */
    public function getAction(ParamFetcherInterface $fetcher, EntityManagerInterface $em) {
        // Get categories
        $categories_id = explode(',', $fetcher->get('categoriesId'));

        $options = [
            'addProfilePicture' => true,
            'addCategories' => true,
        ];

        // Configure limit and order
        if($fetcher->get('limit') !== -1)
            $options['limit'] = $fetcher->get('limit');

        $options['offset'] = $fetcher->get('offset');

        // Configure order
        switch ($fetcher->get('orderBy')) {
            case 'score':
                $options['orderBy'] = 'score';
        }

        $rows = $em->getRepository(Celebrity::class)->findByCategories($categories_id, $options);

        return $rows;
    }

``

但是当我用 Postman 调用我的节点时,我遇到了这个错误:

控制器和方法需要通过 setController 设置

错误来自 ParamFetcher,而该行

$categories_id = explode(',', $fetcher->get('categoriesId'));

你知道这个问题的起源吗:/?

只需在 Symfony 的 config.yml 中启用参数提取器侦听器:

fos_rest:
    param_fetcher_listener: true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM