## 路由回調
Each routing method described above accepts a callback routine as its final argument. This argument can be any PHP callable, and by default it accepts three arguments.
> 上面描述的每個路由方法都接受一個回調例程作為它的最后一個參數。這個參數可以是任何PHP可調用的,默認情況下它接受三個參數。
* `Request`The first argument is a`Psr\Http\Message\ServerRequestInterface`object that represents the current HTTP request.
* `Response`The second argument is a`Psr\Http\Message\ResponseInterface`object that represents the current HTTP response.
* `Arguments`The third argument is an associative array that contains values for the current route’s named placeholders.
> `Request`第一個參數是一個代表當前Http請求的`Psr\Http\Message\ServerRequestInterface`對象。
>
> `Response`第二個參數是一個代表當前Http響應的`Psr\Http\Message\ResponseInterface`對象。
>
> `Arguments`第三個參數是一個關聯數組,它包含當前路由的命名占位符的值。
### 向響應寫入內容
There are two ways you can write content to the HTTP response. First, you can simply `echo()` content from the route callback. This content will be appended to the current HTTP response object. Second, you can return a `Psr\Http\Message\ResponseInterface` object.
有兩種方法可以將內容寫入HTTP響應。首先,您可以簡單地從路由回調`echo()`內容。此內容將附加到當前HTTP響應對象。其次,您可以返回一個`Psr\Http\Message\ResponseInterface`對象。
### Closure binding閉包綁定
If you use a [dependency container](http://www.slimframework.com/docs/v4/concepts/di.html) and a `Closure`instance as the route callback, the closure’s state is bound to the `Container` instance. This means you will have access to the DI container instance `inside` of the Closure via the `$this` keyword:
> 如果使用`依賴容器`和`閉包`實例作為路由回調,則閉包的狀態綁定到`容器`實例。這意味著你可以通過`$This`關鍵字訪問閉包內部的DI容器實例:
~~~php
$app->get('/hello/{name}', function ($request, $response, $args) {
// Use app HTTP cookie service
$this->get('cookies')->set('name', [
'value' => $args['name'],
'expires' => '7 days'
]);
});
~~~
**Heads Up!**
Slim does not support`static`closures.
> **提醒!**
> Slim不支持`靜態`閉包。
## Redirect helper重定向的助手
You can add a route that redirects `GET` HTTP requests to a different URL with the Slim application’s `redirect()` method. It accepts three arguments:
1. The route pattern (with optional named placeholders) to redirect `from`
2. The location to redirect `to` , which may be a `string` or a [Psr\\Http\\Message\\UriInterface](https://www.php-fig.org/psr/psr-7/#35-psrhttpmessageuriinterface)
3. The HTTP status code to use (optional; `302` if unset)
> 您可以使用Slim應用程序的`redirect()`方法添加一個將 `GET`HTTP請求重定向到另一個URL的路由。它接受三個參數:
> 1. 路由模式(帶有可選的命名占位符)來重定向 `from`
> 2. 重定向到的位置,可以是一個 `string`或一個 [Psr\\Http\\Message\\UriInterface](https://www.php-fig.org/psr/psr-7/#35-psrhttpmessageuriinterface)
> 3.要使用的HTTP狀態碼(可選;如果未設置的`302`)
~~~php
$app->redirect('/books', '/library', 301);
~~~
`redirect()` routes respond with the status code requested and a `Location` header set to the second argument.
> `redirect()`路由使用請求的狀態碼和設置到第二個參數的`Location`報頭來響應。
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir