在Symfony 3中不允许使用405方法

mon*_*ser 6 php rest symfony

我有这条路线的路线

/**
 * @Method({"DELETE"})
 * @Route("/secure/users")
 */
Run Code Online (Sandbox Code Playgroud)

当我尝试做一个cUrl

<html>
    <head>
        <meta charset="UTF-8" />
        <title>An Error Occurred: Method Not Allowed</title>
    </head>
    <body>
        <h1>Oops! An Error Occurred</h1>
        <h2>The server returned a "405 Method Not Allowed".</h2>

        <div>
            Something is broken. Please let us know what you were doing when this error occurred.
            We will fix it as soon as possible. Sorry for any inconvenience caused.
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我也尝试启用

Request::enableHttpMethodParameterOverride();
Run Code Online (Sandbox Code Playgroud)

在app.dev和app_dev.php中,我可以处理PUT请求.

cha*_*asr 1

将此参数添加到您的curl请求中:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
Run Code Online (Sandbox Code Playgroud)

或在命令行中

curl -X DELETE "http://localhost/secure/users"
Run Code Online (Sandbox Code Playgroud)

如果您使用 jQuery 执行 XHR 请求,只需执行

$.ajax({
    url: '/secure/users',
    type: 'DELETE',
    data: { id: resourceToDelete }
    success: function(result) {
        // Do something with the result
    }
});
Run Code Online (Sandbox Code Playgroud)

对于纯 javascript :

var req = new XMLHttpRequest();
req.open('DELETE', '/secure/users');
req.setRequestHeader("Content-type", "application/json");
req.send({ id: 'entityIdentifier' });
Run Code Online (Sandbox Code Playgroud)

如果您想通过浏览器访问它或传递查询参数/secure/users?id=x,请使用 GET :

/**
 * @Method({"GET"})
 * @Route("/secure/users")
 */
Run Code Online (Sandbox Code Playgroud)

请参阅PUT 和 DELETE HTTP 请求方法有什么用处?