Search code examples
apisymfonytwigfosrestbundle

"There are no registered paths for namespace "App"." FosRestBundle, Symfony, POST/DELETE


I receive such a message "There are no registered paths for namespace "App"" while doing post or delete requests to my API. GET works. I use FosUserBundle.

config.yml

...
fos_rest:
    body_converter:
      enabled: true
      validate: true
      validation_errors_argument: validationErrors
    exception:
      enabled: true
      exception_controller: 'AppBundle\Controller\ExceptionController::showAction'
    param_fetcher_listener: true
    routing_loader:
          default_format: json
          include_format: false
    serializer:
        serialize_null: true
    view:
        view_response_listener: force
...

PersonRestController.php

...
class PersonRestController extends AbstractController {

    use ControllerTrait;

    /**
     * @Rest\View()
     */
    public function getPersonsAction() {
        $data = $this->getDoctrine()
            ->getRepository(Person::class)
            ->findAll();
        return $data;
    }

    /**
     * @Rest\View(statusCode=201)
     * @ParamConverter("person", converter="fos_rest.request_body")
     * @Rest\NoRoute()
     */
    public function postPersonsAction(Person $person, ConstraintViolationListInterface $validationErrors) {
        if (count($validationErrors) > 0) {
            throw new ValidationException($validationErrors);
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($person);
        $em->flush();

        return $person;
    }

    /**
    * @Rest\View()
    */
    public function deletePersonsAction(?Person $person) {

        if($person === null) {
            return $this->view(null, 404);
        }

        $em = $this->getDoctrine()->getManager();
        $em->remove($person);
        $em->flush();

        return null;
    }

    /**
     * @Rest\View()
     */
    public function getPersonAction(?Person $person) {
        if($person === null) {
            return $this->view(null, 404);
        }

        return $person;
    }
}
...

Error:

error

I've checked my config.yml. It shows no duplicate. The POST method still works despite the error, DELETE doesn't however.


Solution

  • It seems like you used App\ instead of AppBundle\ in a use statement (check also your configuration files).

    Make sure that you have include the following lines in config.yml :

    templating:
        engines: ['twig']
    

    You should also extend FOSRestController instead of using the Trait (FOSRestBundle Step 2: The view layer)

    NB : I'm assuming that you use AppBundle as the main bundle because your using it in config.xml for key exception_controller.