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 withaddGroup().
Basic Syntax
Route groups are created using theaddGroup() method:
Real-World Example
Here’s how route groups are used to organize API endpoints by resource:api.php
How It Works
Projects Group
The/api/projects group contains two routes:
GET /api/projects/testing/test- Calls$obj->hello()GET /api/projects/- Calls$obj->display(1)
api.php
Tasks Group
The/api/tasks group contains:
GET /api/tasks/- Calls$obj->display(2)
api.php
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 theuse($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
Nesting Groups
While not shown in the example, FastRoute supports nested groups:Combining Groups with Individual Routes
You can mix route groups with individual route definitions in the same file:api.php
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
Next Steps
Route Parameters
Add dynamic parameters to your grouped routes
Routing Basics
Review basic routing concepts