bigoen / mercure-twig-bundle
Mercure twig bundle.
Installs: 153
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:php
Requires
- php: >=7.4
- bigoen/mercure-twig: ^1.0
- symfony/framework-bundle: ^4.0 || ^5.0
This package is auto-updated.
Last update: 2025-04-08 23:31:25 UTC
README
Developed based on https://github.com/sroze/live-twig package.
Install:
composer require bigoen/mercure-twig-bundle
If you don't use Symfony Flex:
.env.local
###> bigoen/mercure-twig-bundle ### MERCURE_TWIG_PUBLISH_URL=http://mercure/.well-known/mercure ###< bigoen/mercure-twig-bundle ###
config/bundles.php
return [ // ... Bigoen\MercureTwigBundle\BigoenMercureTwigBundle::class => ['all' => true], ];
config/packages/bigoen_mercure_twig.yaml
bigoen_mercure_twig: public_url: "%env(MERCURE_TWIG_PUBLISH_URL)%" # subscriber_js: '@BigoenMercureTwig\subscriber_js.html.twig'
How to use?
Configurations:
- Set MERCURE_TWIG_PUBLISH_URL,
- Set subscriber_js in yaml configuration. Default: '@BigoenMercureTwig\subscriber_js.html.twig'
Publisher Example:
use Symfony\Component\Mercure\PublisherInterface; use Symfony\Component\Mercure\Update; /** @var PublisherInterface $publisher */ $publisher(new Update('live', 'test'));
Controller Example:
<?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; /** * @Route("/mercure", name="mercure.") */ class MercureController extends AbstractController { /** * @Route("/order", name="order") */ public function orderAction(): Response { return $this->render('mercure/order.html.twig'); } }
Twig Example:
- Set render controller,
- Set mercure topics,
- Set token is private.
- Set isAdd. Default: 0. 1 = afterbegin, 2 = beforebegin.
{{ render_bigoen_mercure_twig( controller('App\\Controller\\MercureController::orderAction'), {'topics': ['live'], 'isAdd': 1, 'token': 'bearer token'} ) }}