Groups

Slim lets you group related routes. This is helpful when you find yourself repeating the same URL segmentsfor multiple routes. This is best explained with an example. Let’s pretend we are building an API forbooks.

  1. <?php
  2. $app = new \Slim\Slim();
  3. // API group
  4. $app->group('/api', function () use ($app) {
  5. // Library group
  6. $app->group('/library', function () use ($app) {
  7. // Get book with ID
  8. $app->get('/books/:id', function ($id) {
  9. });
  10. // Update book with ID
  11. $app->put('/books/:id', function ($id) {
  12. });
  13. // Delete book with ID
  14. $app->delete('/books/:id', function ($id) {
  15. });
  16. });
  17. });

The routes defined above would be accessible at, respectively:

  1. GET /api/library/books/:id
  2. PUT /api/library/books/:id
  3. DELETE /api/library/books/:id

Route groups are very useful to group related routes and avoid repeating common URL segmentsfor each route definition.