多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 路由组 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`的实例