forked from SymfonyCasts/symfony-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercure-publish-message.diff
35 lines (32 loc) · 1.7 KB
/
mercure-publish-message.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php
index a40755b..753be33 100644
--- a/src/Controller/ProductController.php
+++ b/src/Controller/ProductController.php
@@ -12,6 +12,8 @@ use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Mercure\HubInterface;
+use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\UX\Turbo\Stream\TurboStreamResponse;
@@ -70,7 +72,7 @@ class ProductController extends AbstractController
/**
* @Route("/product/{id}/reviews", name="app_product_reviews")
*/
- public function productReviews(Product $product, CategoryRepository $categoryRepository, Request $request, EntityManagerInterface $entityManager)
+ public function productReviews(Product $product, CategoryRepository $categoryRepository, Request $request, EntityManagerInterface $entityManager, HubInterface $mercureHub)
{
$reviewForm = null;
@@ -81,6 +83,12 @@ class ProductController extends AbstractController
if ($request->isMethod('POST')) {
$this->denyAccessUnlessGranted('ROLE_USER');
+ $update = new Update(
+ 'product-reviews',
+ '<turbo-stream action="update" target="product-quick-stats"><template>QUICK STATS CHANGED!</template></turbo-stream>'
+ );
+ $mercureHub->publish($update);
+
$reviewForm->handleRequest($request);
if ($reviewForm->isSubmitted() && $reviewForm->isValid()) {