From a3555b019fa306be59b55006e1d4da44dde261bb Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Thu, 4 Jan 2024 10:02:33 -0700 Subject: [PATCH] Added documentation to readme on grouping --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++ tests/FlightTest.php | 6 +++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 79b02f2..c8f25c0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 8254e60..504ebc1 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -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(); + } }