Skip to content

Commit 14a92cd

Browse files
authored
chore(docs): add optional parameters section to router documentation (#1714)
1 parent 3c54c30 commit 14a92cd

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

docs/1-essentials/01-routing.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,85 @@ final readonly class AircraftController
4949
}
5050
```
5151

52+
### Optional parameters
53+
54+
Sometimes you may want a route to match both with and without a parameter. For instance, you might want `/aircraft` to show all aircraft, and `/aircraft/123` to show a specific aircraft. This can be achieved by marking route parameters as optional.
55+
56+
To mark a parameter as optional, prefix it with a question mark `?` inside the curly braces. The corresponding method parameter must either be nullable or have a default value.
57+
58+
```php app/AircraftController.php
59+
use Tempest\Router\Get;
60+
use Tempest\View\View;
61+
use function Tempest\view;
62+
63+
final readonly class AircraftController
64+
{
65+
#[Get(uri: '/aircraft/{?id}')]
66+
public function index(?string $id): View
67+
{
68+
if ($id === null) {
69+
// Show all aircraft
70+
$aircraft = $this->aircraftRepository->all();
71+
} else {
72+
// Show specific aircraft
73+
$aircraft = $this->aircraftRepository->find($id);
74+
}
75+
76+
return view('aircraft.view.php', aircraft: $aircraft);
77+
}
78+
}
79+
```
80+
81+
In this example, both `/aircraft` and `/aircraft/123` will match the same route. When the parameter is not provided, the method parameter receives `null`.
82+
83+
Alternatively, you may provide a default value instead of using a nullable type:
84+
85+
```php app/AircraftController.php
86+
#[Get(uri: '/aircraft/{?type}')]
87+
public function filter(string $type = 'all'): View
88+
{
89+
// When /aircraft is requested, $type will be 'all'
90+
// When /aircraft/commercial is requested, $type will be 'commercial'
91+
}
92+
```
93+
94+
You may also combine required and optional parameters. Optional parameters should come after required ones:
95+
96+
```php app/FlightController.php
97+
use Tempest\Router\Get;
98+
use Tempest\View\View;
99+
use function Tempest\view;
100+
101+
final readonly class FlightController
102+
{
103+
#[Get(uri: '/flights/{id}/{?segment}')]
104+
public function show(string $id, ?string $segment): View
105+
{
106+
// Matches both /flights/AA123 and /flights/AA123/departure
107+
}
108+
}
109+
```
110+
111+
Multiple optional parameters are also supported:
112+
113+
```php app/AircraftController.php
114+
#[Get(uri: '/aircraft/{?manufacturer}/{?model}')]
115+
public function search(?string $manufacturer, ?string $model): View
116+
{
117+
// Matches /aircraft, /aircraft/cessna, and /aircraft/cessna/172
118+
}
119+
```
120+
121+
Optional parameters work seamlessly with [regular expression constraints](#regular-expression-constraints). Simply add the regex pattern after the parameter name:
122+
123+
```php app/AircraftController.php
124+
#[Get(uri: '/aircraft/{?id:\d+}')]
125+
public function show(?int $id): View
126+
{
127+
// Matches /aircraft and /aircraft/123 (numeric only)
128+
}
129+
```
130+
52131
### Regular expression constraints
53132

54133
You may constrain the format of a route parameter by specifying a regular expression after its name.

0 commit comments

Comments
 (0)