简体   繁体   English

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

[英]How to handle PUT/DELETE requests in Symfony2 FOSRestBundle

When I use PUT or DELETE requests on rest route generate by FOSRestBundle , it returns 当我在FOSRestBundle生成的其余路由上使用PUTDELETE请求时,它返回

Forbidden 禁止的

You don't have permission to access /app_dev.php/api/resources/3 on this server. 您无权访问此服务器上的/app_dev.php/api/resources/3。 Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. 此外,在尝试使用ErrorDocument处理请求时遇到403禁止错误。

When I use GET or POST then it works just fine! 当我使用GETPOST它就可以正常工作!

Here is my rest config: 这是我的其余配置:

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

And my rest controller: 我的休息控制器:

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

    return array(1, 2, 3);
}

It throws 403 only when I use putResourceAction or deleteResourceAction . 仅当我使用putResourceActiondeleteResourceAction时,它将引发403

Thanks for any help! 谢谢你的帮助!

If anyone is experiencing this kind of problem with PUT, DELETE or other methods unavailable, try adding this to .htaccess: 如果有人遇到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>

Other way described here to allow only specified methods 此处描述的其他方式仅允许指定的方法

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

Please, check allowed methods in your web server configuration. 请检查您的Web服务器配置中允许的方法。

Information for apache apache的信息

For Nginx 对于Nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM