## Route strategies路由策略
The route callback signature is determined by a route strategy. By default, Slim expects route callbacks to accept the request, response, and an array of route placeholder arguments. This is called the RequestResponse strategy. However, you can change the expected route callback signature by simply using a different strategy. As an example, Slim provides an alternative strategy called `RequestResponseArgs` that accepts request and response, plus each route placeholder as a separate argument.
> 路由回調簽名由路由策略決定。
>
> 默認情況下,Slim期望路由回調接受請求、響應和路由占位符參數數組。
>
> 這稱為RequestResponse策略。
>
> 但是,您可以通過簡單地使用不同的策略來更改預期的路由回調簽名。
>
> 例如,Slim提供了一個名為`RequestResponseArgs`的替代策略,該策略接受請求和響應,并將每個路由占位符作為單獨的參數。
Here is an example of using this alternative strategy:
> 下面是一個使用這種替代策略的例子:
~~~php
<?php
use Slim\Factory\AppFactory;
use Slim\Handlers\Strategies\RequestResponseArgs;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
/**
* 更改RouteCollector組件上的默認調用策略
* 在應用此更改后,將為每個定義的路由更改它
* Changing the default invocation strategy on the RouteCollector component
* will change it for every route being defined after this change being applied
*/
$routeCollector = $app->getRouteCollector();
$routeCollector->setDefaultInvocationStrategy(new RequestResponseArgs());
$app->get('/hello/{name}', function ($request, $response, $name) {
return $response->write($name);
});
~~~
Alternatively you can set a different invocation strategy on a per route basis:
> 或者,您可以根據每個路由設置不同的調用策略:
~~~php
<?php
use Slim\Factory\AppFactory;
use Slim\Handlers\Strategies\RequestResponseArgs;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$routeCollector = $app->getRouteCollector();
$route = $app->get('/hello/{name}', function ($request, $response, $name) {
return $response->write($name);
});
$route->setInvocationStrategy(new RequestResponseArgs());
~~~
You can provide your own route strategy by implementing the`Slim\Interfaces\InvocationStrategyInterface`.
> 可以通過實現`Slim\Interfaces\InvocationStrategyInterface`來提供您自己的路由策略
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir