1

I have been creating REST API using symfony4, I have installed FosRestBundle and I configured it to return object directly from the controller.

I want to return an object from the controller but I get the error

Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given

MoviesController:

/**
 * @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 }
3
  • You've disabled the SensioFrameworkExtra annotation for the view and this is wrong. You have to enable it. try this : view: { annotations: true } Commented Mar 23, 2018 at 20:54
  • I have enabled it, but still not working. Commented Mar 24, 2018 at 9:39
  • Annotation system must be enabled indeed. Verify your annotation of view (maybe a typo ? You didnt paste the declaration). Last solution to me is to dump the result of this condition that does not act as expected in your case github.com/FriendsOfSymfony/FOSRestBundle/blob/… Commented May 16, 2018 at 15:00

1 Answer 1

1

I use FOSRest, but my controller looks like:

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.