You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -169,11 +170,11 @@ For a complete list of available environment variables, see the plugin's `config
169
170
170
171
The plugin provides multiple integration points depending on your use case:
171
172
172
-
- **Templates**: Use the `MercureHelper` for the easiest, self-contained integration (handles authorization and URLs)
173
-
- **Controllers**: Use the `MercureComponent` for centralized authorization and separation of concerns (recommended for testability)
174
-
- **Services/Commands**: Use the `Publisher` facade for publishing updates
175
-
- **Manual Control**: Use the `Authorization` facade when you need direct response manipulation
173
+
- **Controllers**: Use the `MercureComponent` to centrally manage both authorization and subscriptions as topics
174
+
- **Templates**: Use the `MercureHelper` to generate Mercure topic URLs for EventSource subscriptions in your views and templates.
175
+
- **Services & Manual Control**: Use the `Publisher` facade to publish updates and the `Authorization` facade for direct response manipulation when you need lower-level control (e.g., outside controllers/views, such as in background jobs or custom middleware).
176
176
177
+
> [!TIP]
177
178
> **Note:** Facades (`Publisher`, `Authorization`) can be used in any context where a CakePHP component or helper does not fit, such as in queue jobs, commands, models, or other non-HTTP or background processing code. This makes them ideal for use outside of controllers and views.
178
179
179
180
### Choosing Your Authorization Strategy
@@ -182,15 +183,10 @@ Pick the approach that best fits your workflow:
182
183
183
184
| Scenario | Recommended Approach | Method to Use |
| **Authorize in controller, display URL in template** | `MercureComponent` + `url()` | `$this->Mercure->authorize()` in controller, `$this->Mercure->url($topics)` in template |
186
-
| **Authorize directly in template** | `MercureHelper::url()` with `subscribe` | `$this->Mercure->url($topics, $subscribe)` |
187
-
| **Public topics (no authorization)** | `url()` | `$this->Mercure->url($topics)` |
186
+
| **Authorize in controller, display URL in template** | `MercureComponent` + `MercureHelper` | `$this->Mercure->authorize()` in controller, `$this->Mercure->url($topics)` in template |
187
+
| **Public topics (no authorization)** | `MercureHelper` | `$this->Mercure->url($topics)` |
> * **Easiest:** Use `MercureHelper::url($topics, $subscribe)` directly in templates for quick setup.
192
-
> * **Best Practice:** For larger applications, handle authorization in controllers using `MercureComponent`, then use `url($topics)` in templates. This keeps authorization logic centralized and testable.
193
-
194
190
### Publishing Updates
195
191
196
192
Use the `Publisher` facade to send updates to the Mercure hub:
@@ -405,11 +401,6 @@ Publisher::publish($update);
405
401
406
402
The plugin provides a View Helper to generate Mercure URLs in your templates.
407
403
408
-
> [!IMPORTANT]
409
-
> **Authorization Consideration:** When using the `MercureHelper`, understand the difference:
The special topic `*` matches all updates (use with caution in production).
480
-
481
470
## Authorization
482
471
483
472
### Publishing Private Updates
@@ -498,9 +487,9 @@ Private updates are only delivered to subscribers with valid JWT tokens containi
498
487
499
488
### Setting Authorization Cookies
500
489
501
-
#### Using the Component (Recommended for Separation of Concerns)
490
+
#### Using the Component
502
491
503
-
For centralized, testable authorization logic, use the `MercureComponent` in controllers. Topics added via the component are automatically available in your views:
492
+
For centralized authorization logic, use the `MercureComponent` in controllers. Topics added via the component are automatically available in your views:
504
493
505
494
```php
506
495
classBooksControllerextendsAppController
@@ -548,7 +537,7 @@ class BooksController extends AppController
548
537
}
549
538
```
550
539
551
-
The component provides separation of concerns (authorization in controller, URLs in template) and is fully testable. You can also enable automatic discovery headers:
540
+
The component provides separation of concerns (authorization in controller, URLs in template). You can also enable automatic discovery headers:
552
541
553
542
```php
554
543
// In AppController
@@ -587,40 +576,6 @@ $this->Mercure
587
576
->discover();
588
577
```
589
578
590
-
#### Using the View Helper (Easiest)
591
-
592
-
For the simplest, self-contained approach, use the `MercureHelper::url()` method with the `subscribe` parameter. This **automatically handles both authorization and URL generation** in one call:
593
-
594
-
```php
595
-
// In your template
596
-
<script>
597
-
// url() with subscribe parameter SETS AUTHORIZATION COOKIE
> **Separation of Concerns:** The `url()` method **only sets authorization when the `$subscribe` parameter is provided**. If you're already handling authorization in your controller (via `MercureComponent`), simply omit the `$subscribe` parameter:
615
-
>
616
-
>```php
617
-
> // Authorization already set in controller
618
-
> $this->Mercure->authorize(['/books/123']);
619
-
>
620
-
> // In template: just get the URL (no duplicate authorization)
You can configure default topics that will be automatically merged with any topics you provide to `url()`. This is useful when you want certain topics (like notifications or global alerts) to be included in every subscription:
@@ -641,6 +596,25 @@ public function initialize(): void
641
596
}
642
597
```
643
598
599
+
> [!NOTE]
600
+
> You can also set default topics using the `MercureComponent` in your controller:
601
+
>
602
+
> ```php
603
+
>// In your controller
604
+
>publicfunctioninitialize(): void
605
+
> {
606
+
> parent::initialize();
607
+
> $this->loadComponent('Mercure.Mercure', [
608
+
>'defaultTopics'=> [
609
+
>'https://example.com/notifications',
610
+
>'https://example.com/alerts'
611
+
> ]
612
+
> ]);
613
+
> }
614
+
>```
615
+
>
616
+
> This will make the default topics available to the helper in your views as well.
617
+
644
618
Now every call to `url()` will automatically include these default topics:
// Result includes: /notifications, /alerts, /user/{id}/messages, /books/456, /comments/789, AND /books/123
661
635
```
662
636
663
-
#### Using the Facade(Alternative)
637
+
#### Using the Facade classes
664
638
665
639
For more control or when not using controllers, you can use the `Authorization` facade directly:
666
640
@@ -688,13 +662,13 @@ The cookie must be set before establishing the EventSource connection. The Mercu
688
662
689
663
The Mercure protocol supports automatic hub discovery via HTTP Link headers. This allows clients to discover the hub URL without hardcoding it, making your application more flexible and following the Mercure specification.
690
664
691
-
### Using the View Helper
665
+
### Using the Component
692
666
693
-
Add the discovery header in your templates or layouts:
667
+
Add the discovery header from your controller using the `MercureComponent`:
694
668
695
669
```php
696
-
// In your layout or template
697
-
<?php $this->Mercure->discover(); ?>
670
+
// In your controller action
671
+
$this->Mercure->discover();
698
672
```
699
673
700
674
This adds a `Link` header to the response:
@@ -718,26 +692,9 @@ fetch('/api/resource')
718
692
});
719
693
```
720
694
721
-
### Using the Facade
722
-
723
-
You can also add the discovery header manually from controllers:
0 commit comments