## 请求方法
Every HTTP request has a method that is typically one of:
* GET
* POST
* PUT
* DELETE
* HEAD
* PATCH
* OPTIONS
You can inspect the HTTP request’s method with the Request object method appropriately named`getMethod()`.
> 您可以使用适当命名为`getMethod()`的请求对象方法来检查HTTP请求的方法。
~~~php
$method = $request->getMethod();
~~~
It is possible to fake or*override*the HTTP request method. This is useful if, for example, you need to mimic a`PUT`request using a traditional web browser that only supports`GET`or`POST`requests.
> 可以伪造或`覆盖`HTTP请求方法。例如,如果您需要使用只支持`GET`或`POST`请求的传统web浏览器来模拟`PUT`请求,这是非常有用的。
>
**Heads Up!**
**注意!**
To enable request method overriding the [Method Overriding Middleware ](https://www.kancloud.cn/dehuadong/slim4/1331999) must be injected into your application.
> 要启用请求方法覆盖,必须将`方法覆盖中间件`注入到应用程序中。
>
There are two ways to override the HTTP request method. You can include a`METHOD`parameter in a`POST`request’s body. The HTTP request must use the`application/x-www-form-urlencoded`content type.
> 有两种方法覆盖HTTP请求方法。您可以在`POST`请求的正文中包含一个`方法`参数。HTTP请求必须使用`application/x-www-form-urlencoded`内容类型。
~~~bash
POST /path HTTP/1.1
Host: example.com
Content-type: application/x-www-form-urlencoded
Content-length: 22
data=value&_METHOD=PUT
~~~
Figure 3: Override HTTP method with \_METHOD parameter.
You can also override the HTTP request method with a custom`X-Http-Method-Override`HTTP request header. This works with any HTTP request content type.
> 您还可以使用自定义的`X-Http-Method-Override`HTTP请求头来覆盖HTTP请求方法。这适用于任何HTTP请求内容类型。
~~~bash
POST /path HTTP/1.1
Host: example.com
Content-type: application/json
Content-length: 16
X-Http-Method-Override: PUT
{"data":"value"}
~~~
Figure 4: Override HTTP method with X-Http-Method-Override header.
## The Request URI 请求URI
Every HTTP request has a URI that identifies the requested application resource. The HTTP request URI has several parts:
> 每个HTTP请求都有一个URI来标识所请求的应用程序资源。HTTP请求URI有几个部分:
* Scheme (e.g.`http`or`https`)
* Host (e.g.`example.com`)
* Port (e.g.`80`or`443`)
* Path (e.g.`/users/1`)
* Query string (e.g.`sort=created&dir=asc`)
You can fetch the PSR-7 Request object’s[URI object](http://www.php-fig.org/psr/psr-7/#3-5-psr-http-message-uriinterface)with its`getUri()`method:
> 你可以用它的`getUri()`方法获取PSR-7请求对象的[URI object](http://www.php-fig.org/psr/psr-7/#3-5-psr-http-message-uriinterface):
~~~php
$uri = $request->getUri();
~~~
The PSR-7 Request object’s URI is itself an object that provides the following methods to inspect the HTTP request’s URL parts:
> PSR-7请求对象的URI本身就是一个对象,它提供以下方法来检查HTTP请求的URL部分:
* getScheme()
* getAuthority()
* getUserInfo()
* getHost()
* getPort()
* getPath()
* getBasePath()
* getQuery()(returns the full query string, e.g.`a=1&b=2`)
* getFragment()
* getBaseUrl()
You can get the query parameters as an associative array on the Request object using`getQueryParams()`.
> 可以使用`getQueryParams()`将查询参数作为请求对象上的关联数组获取。
**Base Path**
If your Slim application's front-controller lives in a physical subdirectory beneath your document root directory, you can fetch the HTTP request's physical base path (relative to the document root) with the Uri object's`getBasePath()`method. This will be an empty string if the Slim application is installed in the document root's top-most directory.
> **Base Path**
>
> 如果Slim应用程序的前端控制器位于文档根目录下的物理子目录中,则可以使用Uri对象的`getBasePath()`方法获取HTTP请求的物理基路径(相对于文档根目录)。如果Slim应用程序安装在文档根目录的最上面的目录中,那么这将是一个空字符串。
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir