Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

#[ApiResource] with mercure: true

#[ApiResource] with mercure: true

~12 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

WITH the hub from chapter 67 in place, enabling it for ONE resource is TRIVIAL – ONE parameter suffices, EXACTLY in the style of the existing #[ApiResource] configuration.

Enabling Mercure

#[ApiResource(
    mercure: true,
    normalizationContext: ['groups' => ['project:read']],
    denormalizationContext: ['groups' => ['project:write']]
)]

FROM NOW ON, API Platform AUTOMATICALLY publishes an update to the hub AS SOON AS a Project gets created, changed, or deleted – WITHOUT a SINGLE line of custom publishing code.

Understanding the topic

BY DEFAULT, the topic is the resource's IRI: https://localhost/api/projects/1 for ONE single project – a client that subscribes to EXACTLY THIS topic receives ONLY updates for THIS project.

Updates at the collection level

#[ApiResource(
    mercure: ['private' => false],
    // ...
)]

Instead of true, mercure ALSO accepts a CONFIGURATION array – 'private' => false makes updates PUBLICLY subscribable (WITHOUT JWT authorization), which is NOT the right choice for OUR project with private, user-bound projects (block 6), shown here ONLY for COMPLETENESS. Chapter 70 shows the PRIVATE, authorized variant.

What exactly gets published

API Platform publishes the COMPLETE, SERIALIZED state of the resource (EXACTLY like a normal GET response, including normalizationContext groups from chapter 23) – the client does NOT have to reconcile the message against the API itself to get the CURRENT data.

Tipp: mercure: true can ALSO be enabled on Task and Tag – for OUR project, the following chapters focus on Project, the SAME pattern transfers 1:1 to ANY other resource.