Added documentation to readme on grouping

pull/506/head
Austin Collier 1 year ago
parent 12073629cc
commit a3555b019f

@ -291,6 +291,61 @@ Flight::route('/', function(\flight\net\Route $route) {
}, true);
```
## Route Grouping
There may be times when you want to group related routes together (such as `/api/v1`).
You can do this by using the `group` method:
```php
Flight::group('/api/v1', function () {
Flight::route('/users', function () {
// Matches /api/v1/users
});
Flight::route('/posts', function () {
// Matches /api/v1/posts
});
});
```
You can even nest groups of groups:
```php
Flight::group('/api', function () {
Flight::group('/v1', function () {
Flight::route('/users', function () {
// Matches /api/v1/users
});
Flight::route('/posts', function () {
// Matches /api/v1/posts
});
});
Flight::group('/v2', function () {
Flight::route('/users', function () {
// Matches /api/v2/users
});
});
});
```
### Grouping with Object Context
You can still use route grouping with the `Engine` object in the following way:
```php
$app = new \flight\Engine();
$app->group('/api/v1', function (Router $router) {
$router->map('/users', function () {
// Matches /api/v1/users
});
$router->map('/posts', function () {
// Matches /api/v1/posts
});
});
```
# Extending
Flight is designed to be an extensible framework. The framework comes with a set

@ -102,9 +102,10 @@ class FlightTest extends PHPUnit\Framework\TestCase
echo 'test';
});
Flight::request()->url = '/test';
$this->expectOutputString('test');
Flight::start();
$this->expectOutputString('test');
}
public function testStaticRouteGroup() {
@ -114,8 +115,9 @@ class FlightTest extends PHPUnit\Framework\TestCase
});
});
Flight::request()->url = '/group/test';
Flight::start();
$this->expectOutputString('test');
Flight::start();
}
}

Loading…
Cancel
Save