Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MonishAMPT/fastroute-code/llms.txt

Use this file to discover all available pages before exploring further.

What Are Route Groups?

Route groups allow you to organize routes that share a common prefix. This is particularly useful for API versioning, admin sections, or any logical grouping of endpoints. Instead of repeating the same prefix for each route, you can define it once with addGroup().

Basic Syntax

Route groups are created using the addGroup() method:
$r->addGroup('/prefix', function (RouteCollector $r) {
    // Routes defined here will have /prefix prepended
});

Real-World Example

Here’s how route groups are used to organize API endpoints by resource:
api.php
$r->addGroup('/api/projects', function (FastRoute\RouteCollector $r)use($obj) {
    $r->get('/testing/test', function()use($obj){
        $obj->hello();
    });
    $r->get("/",function()use($obj){
        $obj->display(1);
    });
});
$r->addGroup('/api/tasks', function (FastRoute\RouteCollector $r)use($obj) {

    $r->get("/",function()use($obj){
        $obj->display(2);
    });
});

How It Works

Projects Group

The /api/projects group contains two routes:
  1. GET /api/projects/testing/test - Calls $obj->hello()
  2. GET /api/projects/ - Calls $obj->display(1)
api.php
$r->addGroup('/api/projects', function (FastRoute\RouteCollector $r)use($obj) {
    $r->get('/testing/test', function()use($obj){
        $obj->hello();
    });
    $r->get("/",function()use($obj){
        $obj->display(1);
    });
});

Tasks Group

The /api/tasks group contains:
  1. GET /api/tasks/ - Calls $obj->display(2)
api.php
$r->addGroup('/api/tasks', function (FastRoute\RouteCollector $r)use($obj) {

    $r->get("/",function()use($obj){
        $obj->display(2);
    });
});
The group prefix is automatically prepended to all routes defined within the group callback.

Benefits of Route Groups

DRY Principle

Don’t repeat the same prefix for every route

Organization

Logically group related endpoints together

Maintainability

Change the prefix in one place to update all routes

Readability

Easier to understand route structure at a glance

Using Variables in Groups

Notice the use($obj) clause in the examples. This allows you to pass external variables into the group closure and make them available to route handlers:
api.php
$r->addGroup('/api/projects', function (FastRoute\RouteCollector $r)use($obj) {
    $r->get('/testing/test', function()use($obj){
        $obj->hello();
    });
});
The use() clause is PHP’s way of capturing variables from the parent scope. You need to use it both on the group closure and the route handler closure.

Nesting Groups

While not shown in the example, FastRoute supports nested groups:
$r->addGroup('/api', function (RouteCollector $r) {
    $r->addGroup('/v1', function (RouteCollector $r) {
        // This route becomes: /api/v1/users
        $r->get('/users', 'get_users');
    });
});

Combining Groups with Individual Routes

You can mix route groups with individual route definitions in the same file:
api.php
// Route groups
$r->addGroup('/api/projects', function (FastRoute\RouteCollector $r)use($obj) {
    $r->get('/testing/test', function()use($obj){
        $obj->hello();
    });
});

// Individual routes
$r->addRoute('GET', '/api/test', 'test_get');
$r->addRoute('POST', '/api/test', 'test_post');

When to Use Route Groups

Use route groups when:
  • Multiple routes share the same prefix (e.g., /api/users/*)
  • You’re implementing API versioning (e.g., /api/v1/*, /api/v2/*)
  • You have admin or protected sections (e.g., /admin/*)
  • You want to apply middleware to a specific group of routes
  • You need to organize routes by resource or domain concept
Don’t use route groups for single routes. The overhead isn’t worth it, and it makes your code harder to read.

Next Steps

Route Parameters

Add dynamic parameters to your grouped routes

Routing Basics

Review basic routing concepts

Build docs developers (and LLMs) love