## 請求方法
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 ](http://www.hmoore.net/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