Skip to content

Slim framework 3 – Route Groups

Last updated on February 20, 2018

Slim lets you group related routes. Route groups allow you to share route attributes, such as middleware or URL segments across a large number of routes without needing to define those attributes on each individual route.

This article examines Slim route groups in detail, illustrating how you can use it, let’s pretend we are building an API for books.

This post is part of a series called – Getting Started with Slim Framework 3
How to install Slim Framework 3
Slim Framework 3 – Route Groups
Slim Framework 3 – configure/load database in Slim Framework 3

Route Prefixes – Version your API routes

// API group
$app->group('/api', function ($app) {

    // version1
    $app->group('/v1', function ($request, $response, $args) {

        // Get book with ID
        $app->get('/book/:id', function ($id) {

        });
       
        // other version one routes

    });

 // version2
    $app->group('/v2', function ($app) {

        // Get book with ID
        $app->get('/book/:id', function ($request, $response, $args) {

        });
       
        // other version two routes

    });

});

Route Prefixes – Short your code

Take a look at the below code, each route begins with the /books prefix.

$app->get('/book', function ($request, $response) {
    ...
});

$app->get('/book/:id', function ($request, $response, $args) {
    ...
});

$app->post('/book', function ($request, $response) {
    ...
});

$app->put('/book/:id', function ($request, $response, $args) {
    ...
});

$app->delete('/book/:id', function ($request, $response, $args) {
    ...
});

Now let’s use route group() method and combine routes by their prefix, each subroute only defines the differences like routes with an id and routes without an id. More complex sub-routes are possible, too.


$app->group('/book', function ($app) {
    $app->get('/', function ($request, $response) {
    ...
    });

   $app->get('/:id', function ($request, $response, $args) {
    ...
   });

   $app->post('/', function ($request, $response) {
    ...
   });

   $app->put('/:id', function ($request, $response, $args) {
    ...
   });

   $app->delete('/:id', function ($request, $response, $args) {
    ...
   });
});

Calling middleware on route group

Add middleware to a Route with the Route instance’s add() method. Route middleware is invoked only if its route matches the current HTTP request method and URI. Below is the simple example.

getBody()->write('BEFORE');
    $response = $next($request, $response);
    $response->getBody()->write('AFTER');

    return $response;
};

// API group
$app->group('/api', function ($app) {

    // version1
    $app->group('/v1', function ($request, $response, $args) {

        // Get book with ID
        $app->get('/book/:id', function ($id) {

        });
       
        // other versions one route

    });

})->add($mw);


$app->run();
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments