如何在Symfony2 FOSRestBundle中处理PUT / DELETE请求

时间:2016-07-15 07:10:23

标签: php rest symfony fosrestbundle

当我在PUT生成的休息路线上使用DELETEFOSRestBundle次请求时,会返回

  

禁止

     

您无权访问   此服务器上的/app_dev.php/api/resources/3。另外,一个   尝试使用时遇到403 Forbidden错误   ErrorDocument来处理请求。

当我使用GETPOST时,它的工作正常!

这是我的休息配置:

fos_rest:
    format_listener:
        rules:
            - { prefer_extension: false, priorities: ['json','xml','html'], fallback_format: json }
    view:
        view_response_listener: true
        formats:
            xml:    true
            json:   true
        templating_formats:
            html:   true
        mime_types:
            json:   ['application/json', 'application/x-json']
    routing_loader:
        default_format: json
        include_format: false
    body_listener:  true

我的休息控制器:

/**
 * @Rest\View
 */
public function putResourceAction(Request $request, $id)
{

    return array(1, 2, 3);
}

仅在我使用403putResourceAction时才会抛出deleteResourceAction

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

如果有人遇到PUT,DELETE或其他方法不可用的此类问题,请尝试将其添加到.htaccess:

<RequireAny>
    Require method DELETE GET POST PUT OPTIONS
</RequireAny>

<Limit GET POST PUT DELETE HEAD OPTIONS>
    Order allow,deny
    Allow from all
</Limit>
<LimitExcept GET POST PUT DELETE HEAD OPTIONS>
    Order deny,allow
    Deny from all
</LimitExcept>

以其他方式描述here以仅允许指定的方法

RewriteCond %{REQUEST_METHOD} !^(DELETE|GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]

答案 1 :(得分:-1)

请检查Web服务器配置中允许的方法。

Information for apache

For Nginx