Skip to content

Commit 202036a

Browse files
docs(mercure): compatibility with laravel and improve (#2082)
1 parent 1d00f30 commit 202036a

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

core/mercure.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ Then, the Mercure hub dispatches the updates to all connected clients using [Ser
1616
Mercure support is already installed, configured and enabled in [the API Platform Symfony variant](../symfony/index.md).
1717
If you use the distribution, you have nothing more to do, and you can skip to the next section.
1818

19-
If you have installed API Platform using another method (such as `composer require api`), you need to install [a Mercure hub](https://mercure.rocks/docs/getting-started) and the Symfony MercureBundle.
19+
If you installed API Platform using another method (e.g., `composer require api`), you will need to set up the following:
2020

21-
[Learn how to install and configure MercureBundle manually on the Symfony website](https://symfony.com/doc/current/mercure.html)
21+
1. A [Mercure hub](https://mercure.rocks/docs/getting-started).
22+
23+
2. One of the following, depending on your framework:
24+
- For Symfony users: the [MercureBundle](https://symfony.com/doc/current/mercure.html).
25+
- For Laravel users: the [Laravel Mercure Broadcaster](https://github.com/mvanduijker/laravel-mercure-broadcaster).
2226

2327
## Pushing the API Updates
2428

2529
Use the `mercure` attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:
2630

2731
```php
2832
<?php
29-
// api/src/Entity/Book.php
30-
namespace App\Entity;
33+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
34+
namespace App\ApiResource;
3135

3236
use ApiPlatform\Metadata\ApiResource;
3337

@@ -38,8 +42,8 @@ class Book
3842
}
3943
```
4044

41-
Then, every time an object of this type is created, updated or deleted, the new version is sent to all connected clients through the Mercure hub.
42-
If the resource has been deleted, only the (now deleted) IRI of the resource is sent to the clients.
45+
Then, every time an object of this type is created, updated or deleted, the new version is sent to all connected clients
46+
through the Mercure hub. If the resource has been deleted, only the (now deleted) IRI of the resource is sent to the clients.
4347

4448
In addition, API Platform automatically adds a `Link` HTTP header to all responses related to this resource class.
4549
This header allows smart clients to automatically discover the Mercure hub.
@@ -61,8 +65,8 @@ Then, use options to mark the published updates as privates:
6165

6266
```php
6367
<?php
64-
// api/src/Entity/Book.php
65-
namespace App\Entity;
68+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
69+
namespace App\ApiResource;
6670

6771
use ApiPlatform\Metadata\ApiResource;
6872

@@ -73,12 +77,13 @@ class Book
7377
}
7478
```
7579

76-
It's also possible to execute an _expression_ (using the [Symfony Expression Language component](https://symfony.com/doc/current/components/expression_language.html)), to generate the options dynamically:
80+
It's also possible to execute an _expression_ (using the [Symfony Expression Language component](https://symfony.com/doc/current/components/expression_language.html)),
81+
to generate the options dynamically:
7782

7883
```php
7984
<?php
80-
// api/src/Entity/Book.php
81-
namespace App\Entity;
85+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
86+
namespace App\ApiResource;
8287

8388
use ApiPlatform\Metadata\ApiResource;
8489

@@ -104,27 +109,32 @@ In addition to `private`, the following options are available:
104109

105110
## Dispatching Restrictive Updates (Security Mode)
106111

107-
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber with `topic_selector` to receive only publications that are authorized (partner match).
112+
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber
113+
with `topic_selector` to receive only publications that are authorized (partner match).
108114

109-
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber can use the topic selector `https://example.com/books/{id}` as value of the topic query parameter.
110-
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is only authorized to access some of these resources.
115+
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber
116+
> can use the topic selector `https://example.com/books/{id}` as value of the topic query parameter.
117+
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would
118+
> allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is
119+
> only authorized to access some of these resources.
111120
>
112121
> To solve this problem, the mercure.subscribe claim could contain a topic selector such as: `https://example.com/users/foo/{?topic}`.
113122
>
114-
> The publisher could then take advantage of the previously described behavior by publishing a private update having `https://example.com/books/1` as canonical topic and `https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1` as alternate topic.
123+
> The publisher could then take advantage of the previously described behavior by publishing a private update having
124+
> `https://example.com/books/1` as canonical topic and `https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1` as alternate topic.
115125
>
116126
> [https://mercure.rocks/spec#subscribers](https://mercure.rocks/spec#subscribers)
117127
118128
Below is an example using the `topics` option:
119129

120130
```php
121131
<?php
122-
// api/src/Entity/Book.php
123-
namespace App\Entity;
132+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
133+
namespace App\ApiResource;
124134

125135
use ApiPlatform\Metadata\ApiResource;
126136
use ApiPlatform\Api\UrlGeneratorInterface;
127-
use App\Entity\User;
137+
use App\ApiResource\User;
128138

129139
#[ApiResource(
130140
mercure: [
@@ -153,11 +163,11 @@ Using an _expression_ function:
153163

154164
```php
155165
<?php
156-
// api/src/Entity/Book.php
157-
namespace App\Entity;
166+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
167+
namespace App\ApiResource;
158168

159169
use ApiPlatform\Metadata\ApiResource;
160-
use App\Entity\User;
170+
use App\ApiResource\User;
161171

162172
#[ApiResource(
163173
mercure: 'object.getMercureOptions()',

0 commit comments

Comments
 (0)