Services, security and rate limits work as they do in a controller.
Like·1
Services
public function __construct( private readonlyPostRepository$posts,) {}public function like(MessageBusInterface$bus): void
Security
#[IsGranted('ROLE_USER')]public function like(): void
Rate limits
#[RateLimit('likes')]public function like(): void
06
Onechange.Everyviewer.
#[Broadcast] sends shared data to every viewer through Mercure, and the UI state stays in each browser.
https://phem.dev/posts/123UI server componentsLike·2Comments
phem.devUI server componentsLike·2Comments
phem.devUI server componentsLike·2Comments
Mercure
src/Ui/LikeButton.phem
#[Component, Broadcast]class LikeButton{ #[Prop, BroadcastKey] public Post $post; #[Transactional] public functionlike(): void { ++$this->post->likes; }}
$post->likes
Shared data: every viewer
#[Transactional]
Saved in one transaction: no save() call
07
Compiledbeforeitruns.
phemc compiles each .phem file to plain PHP and Twig at build time.
LikeButton.phemphemc
PHPvar/phem/php/Ui/LikeButton.php
public function render(): Node{ return ( new \Phem\Element('Ui/LikeButton.0.twig', [ 'like' => $this->like(...), 'likes' => $this->likes, ]) );}
Twigvar/phem/templates/Ui/LikeButton.0.twig
<button {{ phem_on('click', like) }}> Like · {{ likes }}</button>