FosRest 405方法不允许

时间:2014-04-01 21:01:37

标签: symfony fosrestbundle

我正在使用Symfony 2和FOSRestBundle。当我把这个指挥官:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X DELETE -d '{"test":{"field":"xxx"}}' http://localhost/tutonew/web/app_dev.php/api/test/4/delete

我遇到此错误: HTTP / 1.1 405方法不允许

我将看到我的源代码:

1-首先我设置配置文件:

#app/config/config.yml sensio_framework_extra: view: annotations: false

fos_rest:

param_fetcher_listener: true
body_listener: true
format_listener: true
view:
    view_response_listener: 'force'

2-在我的AdsAPIBundle下,我只需插入功能:

/**
 * @Rest\View(statusCode=204)
 * @Rest\Delete("/api/test/{id}/delete")
 * 
 */
public function deleteTestAction(Request $id) {

    $repo = $this->getDoctrine()->
            getManager()->
            getRepository("MinnAdsAPIBundle:Test");
    $entity = $repo->find($id);
    if (!$entity) {
        throw $this->createNotFoundException('Unable to find test entity!');
    }

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

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我刚修好这个

public function deleteTestAction(Request $id)

对此:

public function deleteTestAction($id)

最后我发布了这个命令

curl -i -X DELETE -d '{"test":{"field":"xxx"}}' http://localhost/tutonew/web/app_dev.php/api/test/4/delete

我将拥有此代码:

HTTP/1.1 204 No Content