Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api &引用;命名空间“”没有注册路径;应用程序"&引用;FosRestBundle、Symfony、POST/DELETE_Api_Symfony_Twig_Fosrestbundle - Fatal编程技术网

Api &引用;命名空间“”没有注册路径;应用程序"&引用;FosRestBundle、Symfony、POST/DELETE

Api &引用;命名空间“”没有注册路径;应用程序"&引用;FosRestBundle、Symfony、POST/DELETE,api,symfony,twig,fosrestbundle,Api,Symfony,Twig,Fosrestbundle,在对我的API执行post或delete请求时,我收到这样一条消息“没有命名空间“App”的注册路径”。找到工作。我用FosUserBundle config.yml PersonRestController.php 错误: 我已经检查了我的config.yml。它没有重复显示。 尽管有错误,POST方法仍然有效,但是DELETE不起作用。在use语句中,您似乎使用了App\而不是AppBundle\(同时检查您的配置文件) 确保在config.yml中包含以下行: 您还应该扩展FOSRest

在对我的API执行post或delete请求时,我收到这样一条消息“没有命名空间“App”的注册路径”。找到工作。我用FosUserBundle

config.yml

PersonRestController.php

错误:

我已经检查了我的config.yml。它没有重复显示。
尽管有错误,POST方法仍然有效,但是DELETE不起作用。

use
语句中,您似乎使用了
App\
而不是
AppBundle\
(同时检查您的配置文件)

确保在config.yml中包含以下行:

您还应该扩展
FOSRestController
,而不是使用Trait()

NB:我假设您使用
AppBundle
作为主捆绑包,因为您在config.xml中将其用于key
exception\u controller

...
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
...
...
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;
    }
}
...
templating:
    engines: ['twig']