Skip to content

Commit 3193222

Browse files
committed
Add 'Configure components from attributes' use case
1 parent 85c45c5 commit 3193222

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,73 @@ final class IsAdmin implements Voter
301301

302302

303303

304+
### Configure components from attributes
305+
306+
Using attributes simplifies configuration, placing definition closer to the code, where it's used. ICanBoogie's router can be configured automatically from attributes. The following example demonstrates how the `Route` attribute can be used at the class level to define a prefix for the route attributes such as `Get` that are used to tag actions. Action identifiers can be inferred from the controller class and the method names e.g. `skills:list`.
307+
308+
```php
309+
<?php
310+
311+
// …
312+
313+
#[Route('/skills')]
314+
final class SkillController extends ControllerAbstract
315+
{
316+
#[Post]
317+
private function create(): void
318+
{
319+
// …
320+
}
321+
322+
#[Get('.html')]
323+
private function list(): void
324+
{
325+
// …
326+
}
327+
328+
#[Get('/summonable.html')]
329+
private function summonable(): void
330+
{
331+
// …
332+
}
333+
334+
#[Get('/learnable.html')]
335+
private function learnable(): void
336+
{
337+
// …
338+
}
339+
340+
#[Get('/:slug.html')]
341+
private function show(string $slug): void
342+
{
343+
// …
344+
}
345+
}
346+
```
347+
348+
Because the `Get` and `Post` attributes extend `Route`, all action methods can be retrieved with the `filterTargetMethods()` method.
349+
350+
```php
351+
/** @var TargetMethod<Route>[] $target_methods */
352+
$target_methods = Attributes::filterTargetMethods(
353+
fn($attribute) => is_a($attribute, Route::class, true)
354+
);
355+
```
356+
357+
Now then, configuring the router looks as simple as this:
358+
359+
```php
360+
<?php
361+
362+
use ICanBoogie\Binding\Routing\ConfigBuilder;
363+
364+
/* @var ConfigBuilder $config */
365+
366+
$config->from_attributes();
367+
```
368+
369+
370+
304371
## Using Attributes
305372

306373
### Filtering target methods

0 commit comments

Comments
 (0)