企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 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`来提供您自己的路由策略