meloflavio / notificacao-bundle
为 symfony 4 服务的通知。
1.3.3
2020-06-29 20:04 UTC
Requires
- php: ^7.1.3
- lcobucci/jwt: ^3.3
- sonata-project/admin-bundle: ^3.55
- sonata-project/doctrine-orm-admin-bundle: ^3.9
- sonata-project/user-bundle: ^4.5
- stof/doctrine-extensions-bundle: ^1.3
- symfony/config: ^4.3.7|^5.0
- symfony/dependency-injection: ^4.3.7|^5.0
- symfony/http-kernel: ^4.3.7|^5.0
- symfony/mercure-bundle: ^0.2.3
Requires (Dev)
- symfony/phpunit-bridge: ^3.0
Suggests
- doctrine/doctrine-bundle: dev-master
- doctrine/mongodb-odm-bundle: dev-master
README
symfony flex 和 mercure bundle 的集成通知包。
安装
####1. 使用 Composer 需求
composer require meloflavio/notificacao-bundle
####2. 添加环境变量
MERCURE_PUBLISH_URL=https:///mercure/.well-known/mercure MERCURE_JWT_TOKEN=Token MERCURE_SECRET_KEY=CHANGEMEKey
Token 可以在 https://jwt.net.cn/ 上创建
####3. 在 notificacao.yaml 中添加,如果您想更改默认用户类,则是 App/UFT/UserBundle/Entity/Usuario
meloflavio_notificacao: user: class: App/UFT/UserBundle/Entity/Usuario
####4. 在 mercure.yaml 中添加
parameters: mercure_secret_key: '%env(MERCURE_SECRET_KEY)%' mercure_url: '%env(MERCURE_PUBLISH_URL)%' mercure_token: '%env(MERCURE_JWT_TOKEN)%' mercure: enable_profiler: '%kernel.debug%' hubs: default: url: '%env(MERCURE_PUBLISH_URL)%' jwt: '%env(MERCURE_JWT_TOKEN)%'
####5. 创建通知实体
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use MeloFlavio\NotificacaoBundle\Entity\NotificacaoBase; /** * Class Base * @ORM\MappedSuperclass() */ abstract class Notificacao extends NotificacaoBase { /** * @ORM\Column(type="string", length=1500, nullable=true) */ private $texto; /** * * @Gedmo\Blameable(on="create") * @ORM\ManyToOne(targetEntity="Sonata\UserBundle\Model\UserInterface") * @ORM\JoinColumn(name="created_by", referencedColumnName="id") */ protected $createdBy; ... }
####6. 配置路由,创建文件 config/routes/notificacao.yaml
notificacao_discover: resource: '@MeloFlavioNotificacaoBundle/Resources/config/routing/discover.xml' ... ####7. Exemplo de como utilizar Adicione ao templete substituindo {{ settings.data_alvo.id }} pelo id da mensagem ```js <script> document.addEventListener('DOMContentLoaded',function () { function addMessage(data){ let html = "<div " + " <div " + " <span><strong class=\"autor\">"+data.createdBy.username+"</strong> fez um comentário</span>\n" + " </div>\n" + " <div class=\"message-content\">\n"+data.texto+ " </div>\n" + " </div>\n" + " </div>"; document.querySelector("#message-board").firstElementChild.insertAdjacentHTML("afterend",html); } fetch("{{ path('discover') }}").then(result => { const hubUrl = result.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1]; const url = new URL(hubUrl); // let url = new URL("https://:9090/.well-known/mercure"); url.searchParams.append('topic', '/notificcao_base/{{app.user.username}}') const eventSource = new EventSource(url,{ withCredentials: true }); eventSource.onmessage = (event) => { var data = JSON.parse(event.data); addMessage(data); }; }); }) </script>