Symfony2CookBook:如何在路由中使用GET和POST以外的HTTP方法

简介:

How to use HTTP Methods beyond GET and POST in Routes

The HTTP method of a request is one of the requirements that can be checked when seeing if it matches a route. This is introduced in the routing chapter of the book "Routing" with examples using GET and POST. You can also use other HTTP verbs in this way. For example, if you have a blog post entry then you could use the same URL path to show it, make changes to it and delete it by matching on GET, PUT and DELETE.

在查看是否匹配路由时,请求中的HTTP方法将作为请求的一部分而被检查。这在"Routing"书中的路由章节通过使用GET和POST举例介绍过。您通过这样方式来使用其它HTTP动词。例如,如果您有一个博客文章条目,然后您就可以使用相同的URL路径,通过匹配GET、PUT和DELETE来显示、修改和删除它。

1
2
3
4
5
6
7
8
9
10
11
12
blog_show:
     path:     /blog/{slug}
     defaults: { _controller: AcmeDemoBundle:Blog:show }
     methods:   [GET]
blog_update:
     path:     /blog/{slug}
     defaults: { _controller: AcmeDemoBundle:Blog:update }
     methods:   [PUT]
blog_delete:
     path:     /blog/{slug}
     defaults: { _controller: AcmeDemoBundle:Blog: delete  }
     methods:   [DELETE]

Faking the Method with _method(通过_method伪造方法)

The _method functionality shown here is disabled by default in Symfony 2.2 and enabled by default in Symfony 2.3. To control it in Symfony 2.2, you must call Request::enableHttpMethodParameterOverridebefore you handle the request (e.g. in your front controller). In Symfony2.3, use the http_method_override option.

_method 在这里显示的功能在Symfony 2.2中默认是禁用的,而在Symfony 2.3中默认是启用的。在Symfony 2.2中要控制它,您必须在您处理请求之前(如在您的前端控制器中)调用Request::enableHttpMethodParameterOverride。在Symfony 2.3中,则使http_method_override 配置项。


Unfortunately, life isn't quite this simple, since most browsers do notsupport sending PUT and DELETE requests. Fortunately Symfony2 provides youwith a simple way of working around this limitation. By including a _method parameter in the query string or parameters of an HTTP request, Symfony2 willuse this as the method when matching routes. Forms automatically include a hidden field for this parameter if their submission method is not GET or POST.See the related chapter in the forms documentationfor more information.

不幸的是,生活并非如此简单,因为大多数浏览器并不支持发送 PUT 和 DELETE 请求。幸运的是Symfony2 为您提供了一个简单方法来绕过这一限制。通过在HTTP请求中的查询字符串或参数中包含 _method 参数,Symfony2将在匹配路由时将其作为方法使用。如果表单提交方法不是GET或POST时,它们将自动为这一参数创建一个隐藏字段。更多情况请参见: the related chapter in the forms documentation




本文转自 firehare 51CTO博客,原文链接:http://blog.51cto.com/firehare/1257690,如需转载请自行联系原作者
相关文章
|
13天前
|
Android开发 Kotlin
|
10天前
HTTP协议中请求方式GET 与 POST 什么区别 ?
GET和POST的主要区别在于参数传递方式、安全性和应用场景。GET通过URL传递参数,长度受限且安全性较低,适合获取数据;而POST通过请求体传递参数,安全性更高,适合提交数据。
117 2
|
9月前
|
Ubuntu Linux Shell
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
(已成功解决)Linux环境报错—bash: wget: command not found;常见Linux发行版本,Linux中yum、rpm、apt-get、wget的区别;Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
3736 68
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
|
6月前
|
API 开发者
了解 HTTP 的PUT 与 POST方法的综合指南
HTTP PUT 和 POST 方法是构建 Web 应用与 API 的核心工具,用于资源的创建与更新。PUT 方法通过指定 URL 更新或创建完整资源,具有幂等性;而 POST 方法更灵活,主要用于创建新资源,但不具备幂等性。本文详细对比了两者在请求体、URL 使用、资源处理等方面的区别,并提供了实际应用示例,帮助开发者根据场景选择合适的方法以优化 API 设计。
|
6月前
|
缓存 安全 API
为什么 HTTP GET 方法不使用请求体?
本指南深入探讨了为什么HTTP GET方法通常不使用请求体,解释了GET方法的主要用途及其设计原则。GET请求旨在通过URL安全、幂等地检索数据,避免因请求体带来的复杂性和潜在问题。尽管HTTP/1.1规范允许GET请求包含请求体,但这并不常见且可能引发副作用。掌握这些原则有助于开发者在API开发中更高效地使用GET请求。
|
6月前
|
API
掌握 HTTP 请求的艺术:理解 cURL GET 语法
掌握 cURL GET 请求的语法和使用方法是 Web 开发和测试中的基本技能。通过灵活运用 cURL 提供的各种选项,可以高效地与 API 进行交互、调试网络请求,并自动化日常任务。希望本文能帮助读者更好地理解和使用 cURL,提高工作效率和代码质量。
251 7
|
8月前
|
关系型数据库 MySQL Docker
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
|
9月前
|
安全 网络安全 数据安全/隐私保护
内网/局域网IP地址申请https证书方法
为内网/局域网IP地址申请HTTPS证书,可增强数据传输的安全性。首先确定固定的内网IP地址,选择可信的证书颁发机构,注册并申请免费或付费SSL证书,提交相关信息,支付费用(如有)。证书申请成功后,下载并配置于服务器,确保通过浏览器访问时显示为安全连接。注意定期更新证书,确保持续的安全保障。此过程适用于局域网内部通信加密,提升内网服务的安全水平。
|
10月前
|
安全 应用服务中间件 网络安全
免费ip地址https证书申请方法
IP SSL证书用于保障IP地址与浏览器间的数据传输安全,多数需付费购买。JoySSL现提供免费试用版,申请流程包括:访问官网、注册账号(需输入特定注册码230922)、选择证书类型、填写申请信息、验证IP控制权、等待审核、下载及部署证书。确保IP地址独立可控,信息准确,及时续期。
|
Web App开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
可伸缩系统的架构经验 Feb 27th, 2013 | Comments 最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。
2368 0