Json 使用Symfony4的RESTAPI

Json 使用Symfony4的RESTAPI,json,rest,symfony,symfony4,Json,Rest,Symfony,Symfony4,我一直在使用symfony4创建RESTAPI, 我已经安装了FosRestBundle,并将其配置为直接从控制器返回对象 我想从控制器返回一个对象,但我得到了错误 Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given 电影管理员: /** * @Rest\View() * @Route("/movies", name="get_movies") * */ public

我一直在使用symfony4创建RESTAPI, 我已经安装了FosRestBundle,并将其配置为直接从控制器返回对象

我想从控制器返回一个对象,但我得到了错误

Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given
电影管理员:

/**
 * @Rest\View()
 * @Route("/movies", name="get_movies")
 *
 */
public function getMovies()
{
    $movies = $this->getDoctrine()
                    ->getRepository(Movie::class)
                    ->findAll();

    return $movies;

 }
fos_rest.yaml:

fos_rest:
    param_fetcher_listener: force
    body_converter:
        enabled: true
    view:
        formats: { json: true, xml: false, rss: false }
        view_response_listener: true
    serializer:
        serialize_null: true
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: 'json' }
framework.yaml

sensio_framework_extra:
    view: { annotations: false }

我使用FOSRest,但我的控制器如下所示:

use FOS\RestBundle\Controller\FOSRestController;   //
use FOS\RestBundle\View\View;                       // <-- first import
use FOS\RestBundle\Controller\Annotations as Rest; //

class TagController extends FOSRestController      // <-- extend
{
    /**
     * Retrieves a collection of Tag resource.
     *
     * @Rest\View(serializerGroups={"details"})
     * @Rest\Get("/tag")                            // <-- rout
     */
    public function index(TagRepository $tagRepository): View // <-- use View
    {
        $data = $tagRepository->findAll();

        return View::create($data, Response::HTTP_OK);        // <-- how to return
    }
}
使用FOS\RestBundle\Controller\FOSRestController//

使用FOS\RestBundle\View\View;//您已禁用视图的SensioFrameworkExtra注释,这是错误的。你必须启用它。试试这个:视图:{annotations:true}我已经启用了它,但仍然不工作。必须确实启用注释系统。验证您的视图注释(可能是打字错误?您没有粘贴声明)。我的最后一个解决方案是转储这种情况的结果,而这种情况在您的案例中并不像预期的那样起作用