Search code examples
restsymfonyfosrestbundlesymfony-3.3

Symfony FOSRestBundle error : Call to a member function get() on null. [Routing or Config error]


My problem : I've a symfony 3 app. One Bundle for Rest Route (so my bundle is a rest API) and another Bundle who manage the front (Twig + Ajax). I use fos_rest but it seem makes problems.

My config.yml:

fos_rest:
    routing_loader:
        include_format: false
    view:
        view_response_listener: true
    format_listener:
        rules:
            - { path: '^/rest', priorities: ['json'], fallback_format: 'json' }
            - { path: '^/', stop: true }

My routing.yml :

front:
    resource: '@FrontBundle/Controller'
    type:     annotation

api:
    resource: '@AppBundle/Controller'
    type: rest
    prefix: '/rest'

and one basic route in my RestController :

/**
 * @Rest\View()
 * @Rest\Get("/participants")
 *
 * @param Request $request
 * @return mixed
 */
public function getParticipantsAction(Request $request)
{
    $participants = $this->get('doctrine.orm.entity_manager')
        ->getRepository('AppBundle:Participant')
        ->findAll();

    if (empty($participant)) {
        return new JsonResponse(['message' => 'There is no participants'], Response::HTTP_NOT_FOUND);
    }

    return $participants;
}

I test my route with postman and it send me an error Error: Call to a member function get() on null


Solution

  • Try with: $participants = $this->get('doctrine.orm.default_entity_manager')...

    or with $participants = $this->getDoctrine()...