Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/book/v1/tutorials/create-route-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,24 @@ $routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::cla
- **middleware**: required, (array of) request handler(s)/middleware(s)
- **name**: optional, if omitted, it will be set using the pattern **route path**^PUT
- **excludeMiddleware**: optional, (array of) request handler(s)/middleware(s) to exclude for this route

### Append a route that matches **QUERY** requests

```php
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->query('', ResourceHandler::class, 'route-name');
```

If this route does not need to be piped through specific middleware(s), you can specify the fourth argument `$excludeMiddleware` exclude those middlewares at route level (makes sense when there are multiple routes in a group):

```php
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->query('', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
```

`query()` method arguments:

- **path**: required, of type string
- **middleware**: required, (array of) request handler(s)/middleware(s)
- **name**: optional, if omitted, it will be set using the pattern **route path**^QUERY
- **excludeMiddleware**: optional, (array of) request handler(s)/middleware(s) to exclude for this route
15 changes: 14 additions & 1 deletion docs/book/v1/tutorials/create-single-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ $routeCollector->put('/resource/{id}', SomeHandler::class, 'route-name');
- **middleware**: required, (array of) request handler(s)/middleware(s)
- **name**: optional, if omitted, it will be set using the pattern **route path**^PUT

### Create a route that matches **QUERY** requests

```php
$routeCollector->query('/resource', SomeHandler::class, 'route-name');
```

`query()` method arguments:

- **path**: required, of type string
- **middleware**: required, (array of) request handler(s)/middleware(s)
- **name**: optional, if omitted, it will be set using the pattern **route path**^QUERY

## Chain routes to the RouteCollector

To facilitate creating routes, each `$routeCollector` method returns the existing `RouteCollector` instance, so you can chain routes like this:
Expand All @@ -108,5 +120,6 @@ $routeCollector
->get('/order/{id}', SomeHandler::class, 'app::view-order')
->patch('/order/{id}', SomeHandler::class, 'app::update-order')
->post('/order', SomeHandler::class, 'app::create-order')
->put('/order/{id}', SomeHandler::class, 'app::replace-order');
->put('/order/{id}', SomeHandler::class, 'app::replace-order')
->query('/order', SomeHandler::class, 'app::search-order');
```
15 changes: 15 additions & 0 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ public function put(
return $this;
}

public function query(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null,
): RouteCollectorInterface {
$this->routeCollector->route(
$path,
$this->middlewareFactory->prepare($middleware),
['QUERY'],
$name
);

return $this;
}

/**
* @return list<Route>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/RouteCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function put(
?string $name = null,
): RouteCollectorInterface;

public function query(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null,
): RouteCollectorInterface;

/**
* @return list<Route>
*/
Expand Down
16 changes: 16 additions & 0 deletions src/RouteGroupCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ public function put(
return $this;
}

public function query(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null,
array|string $excludeMiddleware = [],
): RouteGroupCollectorInterface {
$this->routeCollector->route(
$this->preparePath($path),
$this->prepareMiddleware($middleware, $excludeMiddleware),
['QUERY'],
$name
);

return $this;
}

/**
* @return list<Route>
*/
Expand Down
7 changes: 7 additions & 0 deletions src/RouteGroupCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function put(
array|string $excludeMiddleware = [],
): RouteGroupCollectorInterface;

public function query(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null,
array|string $excludeMiddleware = [],
): RouteGroupCollectorInterface;

/**
* @return list<Route>
*/
Expand Down
16 changes: 16 additions & 0 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ public static function methodProvider(): array
'generatedName' => 'app::put',
'allowedMethods' => ['PUT'],
],
[
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => null,
'generatedPath' => '/query',
'generatedName' => '/query^QUERY',
'allowedMethods' => ['QUERY'],
],
[
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => 'app::query',
'generatedPath' => '/query',
'generatedName' => 'app::query',
'allowedMethods' => ['QUERY'],
],
];
}

Expand Down
40 changes: 40 additions & 0 deletions test/RouteGroupCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,46 @@ public static function methodProvider(): array
'allowedMethods' => ['PUT'],
'excludeMiddleware' => [MiddlewareInterface::class],
],
[
'groupPrefix' => '/group',
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => null,
'generatedPath' => '/group/query',
'generatedName' => '/group/query^QUERY',
'allowedMethods' => ['QUERY'],
'excludeMiddleware' => [],
],
[
'groupPrefix' => '/group',
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => null,
'generatedPath' => '/group/query',
'generatedName' => '/group/query^QUERY',
'allowedMethods' => ['QUERY'],
'excludeMiddleware' => [MiddlewareInterface::class],
],
[
'groupPrefix' => '/group',
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => 'app::group-query',
'generatedPath' => '/group/query',
'generatedName' => 'app::group-query',
'allowedMethods' => ['QUERY'],
'excludeMiddleware' => [],
],
[
'groupPrefix' => '/group',
'requestMethod' => 'query',
'routePath' => '/query',
'routeName' => 'app::group-query',
'generatedPath' => '/group/query',
'generatedName' => 'app::group-query',
'allowedMethods' => ['QUERY'],
'excludeMiddleware' => [MiddlewareInterface::class],
],
];
}

Expand Down
Loading