@@ -15,9 +15,10 @@ A real-time product search component might look like this:
15
15
// src/Components/ProductSearchComponent.php
16
16
namespace App\Components;
17
17
18
- use Symfony\UX\LiveComponent\LiveComponentInterface ;
18
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent ;
19
19
20
- class ProductSearchComponent implements LiveComponentInterface
20
+ #[AsLiveComponent('product_search')]
21
+ class ProductSearchComponent
21
22
{
22
23
public string $query = '';
23
24
@@ -33,11 +34,6 @@ class ProductSearchComponent implements LiveComponentInterface
33
34
// example method that returns an array of Products
34
35
return $this->productRepository->search($this->query);
35
36
}
36
-
37
- public static function getComponentName(): string
38
- {
39
- return 'product_search';
40
- }
41
37
}
42
38
```
43
39
@@ -103,19 +99,15 @@ Suppose you've already built a basic Twig component:
103
99
// src/Components/RandomNumberComponent.php
104
100
namespace App\Components;
105
101
106
- use Symfony\UX\TwigComponent\ComponentInterface ;
102
+ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent ;
107
103
108
- class RandomNumberComponent implements ComponentInterface
104
+ #[AsTwigComponent('random_number')]
105
+ class RandomNumberComponent
109
106
{
110
107
public function getRandomNumber(): string
111
108
{
112
109
return rand(0, 1000);
113
110
}
114
-
115
- public static function getComponentName(): string
116
- {
117
- return 'random_number';
118
- }
119
111
}
120
112
```
121
113
@@ -127,16 +119,18 @@ class RandomNumberComponent implements ComponentInterface
127
119
```
128
120
129
121
To transform this into a "live" component (i.e. one that
130
- can be re-rendered live on the frontend), change your
131
- component's interface to ` LiveComponentInterface ` :
122
+ can be re-rendered live on the frontend), replace
123
+ component's ` AsTwigComponent ` attribute to ` AsLiveComponent ` :
132
124
133
125
``` diff
134
126
// src/Components/RandomNumberComponent.php
135
127
136
- + use Symfony\UX\LiveComponent\LiveComponentInterface;
128
+ - use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
129
+ + use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
137
130
138
- - class RandomNumberComponent implements ComponentInterface
139
- + class RandomNumberComponent implements LiveComponentInterface
131
+ - #[AsTwigComponent('random_number')]
132
+ - #[AsLiveComponent('random_number')]
133
+ class RandomNumberComponent
140
134
{
141
135
}
142
136
```
@@ -183,7 +177,8 @@ namespace App\Components;
183
177
// ...
184
178
use Symfony\UX\LiveComponent\Attribute\LiveProp;
185
179
186
- class RandomNumberComponent implements LiveComponentInterface
180
+ #[AsLiveComponent('random_number')]
181
+ class RandomNumberComponent
187
182
{
188
183
/** @LiveProp */
189
184
public int $min = 0;
@@ -267,7 +262,7 @@ the `writable=true` option:
267
262
// src/Components/RandomNumberComponent.php
268
263
// ...
269
264
270
- class RandomNumberComponent implements LiveComponentInterface
265
+ class RandomNumberComponent
271
266
{
272
267
- /** @LiveProp() */
273
268
+ /** @LiveProp(writable=true) */
@@ -438,7 +433,7 @@ namespace App\Components;
438
433
// ...
439
434
use Symfony\UX\LiveComponent\Attribute\LiveAction;
440
435
441
- class RandomNumberComponent implements LiveComponentInterface
436
+ class RandomNumberComponent
442
437
{
443
438
// ...
444
439
@@ -503,7 +498,7 @@ namespace App\Components;
503
498
// ...
504
499
use Psr\Log\LoggerInterface;
505
500
506
- class RandomNumberComponent implements LiveComponentInterface
501
+ class RandomNumberComponent
507
502
{
508
503
// ...
509
504
@@ -548,7 +543,7 @@ namespace App\Components;
548
543
// ...
549
544
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
550
545
551
- class RandomNumberComponent extends AbstractController implements LiveComponentInterface
546
+ class RandomNumberComponent extends AbstractController
552
547
{
553
548
// ...
554
549
@@ -684,11 +679,12 @@ use App\Entity\Post;
684
679
use App\Form\PostType;
685
680
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
686
681
use Symfony\Component\Form\FormInterface;
682
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
687
683
use Symfony\UX\LiveComponent\Attribute\LiveProp;
688
- use Symfony\UX\LiveComponent\LiveComponentInterface;
689
684
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
690
685
691
- class PostFormComponent extends AbstractController implements LiveComponentInterface
686
+ #[AsLiveComponent('post_form')]
687
+ class PostFormComponent extends AbstractController
692
688
{
693
689
use ComponentWithFormTrait;
694
690
@@ -715,11 +711,6 @@ class PostFormComponent extends AbstractController implements LiveComponentInter
715
711
// we can extend AbstractController to get the normal shortcuts
716
712
return $this->createForm(PostType::class, $this->post);
717
713
}
718
-
719
- public static function getComponentName(): string
720
- {
721
- return 'post_form';
722
- }
723
714
}
724
715
```
725
716
@@ -875,7 +866,7 @@ action to the component:
875
866
use Doctrine\ORM\EntityManagerInterface;
876
867
use Symfony\UX\LiveComponent\Attribute\LiveAction;
877
868
878
- class PostFormComponent extends AbstractController implements LiveComponentInterface
869
+ class PostFormComponent extends AbstractController
879
870
{
880
871
// ...
881
872
@@ -932,20 +923,16 @@ that is being edited:
932
923
namespace App\Twig\Components;
933
924
934
925
use App\Entity\Post;
926
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
935
927
use Symfony\UX\LiveComponent\Attribute\LiveProp;
936
- use Symfony\UX\LiveComponent\LiveComponentInterface;
937
928
938
- class EditPostComponent implements LiveComponentInterface
929
+ #[AsLiveComponent('edit_post')]
930
+ class EditPostComponent
939
931
{
940
932
/**
941
933
* @LiveProp()
942
934
*/
943
935
public Post $post;
944
-
945
- public static function getComponentName(): string
946
- {
947
- return 'edit_post';
948
- }
949
936
}
950
937
```
951
938
@@ -985,7 +972,7 @@ you can enable it via the `exposed` option:
985
972
``` diff
986
973
// ...
987
974
988
- class EditPostComponent implements LiveComponentInterface
975
+ class EditPostComponent
989
976
{
990
977
/**
991
978
- * @LiveProp(exposed={})
@@ -1020,12 +1007,13 @@ First use the `ValidatableComponentTrait` and add any constraints you need:
1020
1007
1021
1008
``` php
1022
1009
use App\Entity\User;
1010
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1023
1011
use Symfony\UX\LiveComponent\Attribute\LiveProp;
1024
- use Symfony\UX\LiveComponent\LiveComponentInterface;
1025
1012
use Symfony\UX\LiveComponent\ValidatableComponentTrait;
1026
1013
use Symfony\Component\Validator\Constraints as Assert;
1027
1014
1028
- class EditUserComponent implements LiveComponentInterface
1015
+ #[AsLiveComponent('edit_user')]
1016
+ class EditUserComponent
1029
1017
{
1030
1018
use ValidatableComponentTrait;
1031
1019
@@ -1040,11 +1028,6 @@ class EditUserComponent implements LiveComponentInterface
1040
1028
* @Assert\IsTrue()
1041
1029
*/
1042
1030
public bool $agreeToTerms = false;
1043
-
1044
- public static function getComponentName() : string
1045
- {
1046
- return 'edit_user';
1047
- }
1048
1031
}
1049
1032
```
1050
1033
@@ -1063,7 +1046,8 @@ in an action:
1063
1046
``` php
1064
1047
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1065
1048
1066
- class EditUserComponent implements LiveComponentInterface
1049
+ #[AsLiveComponent('edit_user')]
1050
+ class EditUserComponent
1067
1051
{
1068
1052
// ...
1069
1053
0 commit comments