## 路由組
To help organize routes into logical groups, the`Slim\App`also provides a`group()`method. Each group’s route pattern is prepended to the routes or groups contained within it, and any placeholder arguments in the group pattern are ultimately made available to the nested routes:
> 為了幫助將路由組織到邏輯組中,`Slim\App`還提供了一個`group()`方法。
>
> 每個組的路由模式被預先寫入其中包含的路由或組,組模式中的任何占位符參數最終都可用于嵌套路由:
~~~php
$app->group('/users/{id:[0-9]+}', function (RouteCollectorProxy $group) {
$group->map(['GET', 'DELETE', 'PATCH', 'PUT'], '', function ($request, $response, $args) {
// Find, delete, patch or replace user identified by $args['id']
})->setName('user');
$group->get('/reset-password', function ($request, $response, $args) {
// Route for /users/{id:[0-9]+}/reset-password
// Reset the password for user identified by $args['id']
})->setName('user-password-reset');
});
~~~
The group pattern can be empty, enabling the logical grouping of routes that do not share a common pattern.
> 組模式可以為空,從而支持不共享公共模式的路由的邏輯分組。
~~~php
$app->group('', function (RouteCollectorProxy $group) {
$group->get('/billing', function ($request, $response, $args) {
// Route for /billing
});
$group->get('/invoice/{id:[0-9]+}', function ($request, $response, $args) {
// Route for /invoice/{id:[0-9]+}
});
})->add(new GroupMiddleware());
~~~
Note inside the group closure, Slim binds the closure to the container instance.
* inside route closure,`$this`is bound to the instance of`Psr\Container\ContainerInterface`
> 注意,在組閉包內部,Slim將閉包綁定到容器實例。
>
> 在路由閉包內部,`$this`被綁定到`Psr\Container\ContainerInterface`的實例
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir