Skip to content

Handler Groups

Jakob Tapuć edited this page May 18, 2021 · 4 revisions

Introduction

Sometimes you want to group some handlers under a specific prefix. The group() method does exactly that:

$app->routes(function (Routes $routes) {
    $routes->group('/outer', function (Routes $routes) {
        $routes->get('/', /* */);

        $routes->group('/inner', function (Routes $routes) {
            $routes->post('/', /* */);
        });
    });
});

Groups can be arbitrarily nested. In the example above the POST route will be collected as /outer/inner. Groups can use wildcards to create a catch-all solution:

$app->routes(function (Routes $routes) {
    $routes->group('/outer', function (Routes $routes) {
        $routes->group('*', function (Routes $routes) {
            $routes->get('/foo', /* */);
        });
    });
});

Every path in the example above that matches the route /outer/*/foo will be forwarded to the GET handler. In general * represents one entire segment - part of the path between slashes.

Clone this wiki locally