You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -440,26 +440,120 @@ final readonly class ReceiveInteractionController
440
440
}
441
441
```
442
442
443
-
### Group middleware
443
+
##Route decorators (route groups)
444
444
445
-
While Tempest does not provide a way to group middleware, you can easily create your own route attribute that applies or excludes a set of middleware to a route.
445
+
Route decorators are Tempest's way to manage routes in bulk; it's a feature similar to route groups in other frameworks. Route decorators are attributes that implement the {b`\Tempest\Router\RouteDecorator`} interface. A route decorator's task is to make changes or add functionality to whether route it's associated with. Tempest comes with a few built-in route decorators, and you can make your own as well.
In most cases, you'll want to add route decorators to a controller class, so that they are applied to all actions of that class:
448
+
449
+
```php
450
+
use Tempest\Router\Prefix;
451
+
use Tempest\Router\Get;
452
+
453
+
#[Prefix('/api')]
454
+
final readonly class ApiController
450
455
{
451
-
public function __construct(
452
-
public Method $method,
453
-
public string $uri,
454
-
public array $middleware = [],
455
-
public array $without = [],
456
-
) {
457
-
$this->uri = "/api/{$uri}";
458
-
$this->without[] = [
459
-
...$without,
460
-
VerifyCsrfMiddleware::class,
461
-
SetCookieMiddleware::class
462
-
];
456
+
#[Get('/books')]
457
+
public function books(): Response { /* … */ }
458
+
459
+
#[Get('/authors')]
460
+
public function authors(): Response { /* … */ }
461
+
}
462
+
```
463
+
464
+
However, route decorators may also be applied to individual controller actions:
465
+
466
+
```php
467
+
use Tempest\Router\Stateless;
468
+
use Tempest\Router\Get;
469
+
470
+
final readonly class BlogPostController
471
+
{
472
+
#[Stateless]
473
+
#[Get('/rss')]
474
+
public function rss(): Response { /* … */ }
475
+
}
476
+
```
477
+
478
+
### Built-in route decorators
479
+
480
+
These route decorators are provided by Tempest:
481
+
482
+
#### `#[Stateless]`
483
+
484
+
When you're building API endpoints, RSS feeds, or any other kind of page that does not require any cookie or session data, you may use the {b`#[Tempest\Router\Stateless]`} attribute, which will remove all state-related logic:
final readonly class StatelessController { /* … */ }
539
+
```
540
+
541
+
### Custom route decorators
542
+
543
+
Building your own route decorators is done by implementing the {b`\Tempest\Router\RouteDecorator`} interface and marking your decorator as an attribute.
final readonly class Auth implements RouteDecorator
551
+
{
552
+
public function decorate(Route $route): Route
553
+
{
554
+
$route->middleare[] = AuthMiddleware::class;
555
+
556
+
return $route;
463
557
}
464
558
}
465
559
```
@@ -631,23 +725,6 @@ final class ErrorResponseProcessor implements ResponseProcessor
631
725
}
632
726
```
633
727
634
-
## Stateless routes
635
-
636
-
When you're building API endpoints, RSS pages, or any other kind of page that does not require any cookie or session data, you may use the `{#[Tempest\Router\Stateless]}` attribute, which will remove all state-related logic:
637
-
638
-
```php
639
-
use Tempest\Router\Stateless;
640
-
use Tempest\Router\Get;
641
-
642
-
final readonly class JsonController
643
-
{
644
-
#[Stateless]
645
-
#[Get('/json')]
646
-
public function json(string $path): Response
647
-
{ /* … */ }
648
-
}
649
-
```
650
-
651
728
## Custom route attributes
652
729
653
730
It is often a requirement to have a bunch of routes following the same specifications—for instance, using the same middleware, or the same URI prefix.
0 commit comments