尝试在事件订户中使用通知器

发布于 2025-02-02 19:22:33 字数 1304 浏览 3 评论 0原文

我正在学习Symfony的活动订阅者,我的想法是当我创建新的博客文章时,我想发送通知。我已经能够创建最后的实体。 我添加了代码以获取Symfony Notifier,并能够在Disocrd上发送通知。我目前有以下代码:

class EasyAdminSubscriber implements EventSubscriberInterface
{


    public static function getSubscribedEvents()
    {
        return [
            AfterEntityPersistedEvent::class => ['setBlogPostSlug'],
        ];
    }

    /**
     * @param AfterEntityPersistedEvent $event
     * @param ChatterInterface $chatter
     * @return void
     * @throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
     */
    public function setBlogPostSlug(AfterEntityPersistedEvent $event)
    {
        $chatter = new Chatter();
        $entity = $event->getEntityInstance();

        if (!($entity instanceof Blog)) {
            return;
        }

        $message = (new ChatMessage('You got a new invoice for 15 EUR.'));
            // if not set explicitly, the message is send to the
            // default transport (the first one configured)
        $chatter->send($message);

    }
}

但是,当我运行代码时,我会有lasterinf错误:

App\EventSubscriber\EasyAdminSubscriber::setBlogPostSlug(): Argument #2 ($chatter) must be of type Symfony\Component\Notifier\ChatterInterface, string given

我不明白怎么了。你知道为什么吗?以及如何使事情起作用?

I'm learnig the event Subscriber on Symfony, my idea is when I create a new blog post, I want to send a notification. I'm already able to get the last entity created.
I've added to code to get the Symfony notifier and be able to send a notification on Disocrd. I currently have the following code :

class EasyAdminSubscriber implements EventSubscriberInterface
{


    public static function getSubscribedEvents()
    {
        return [
            AfterEntityPersistedEvent::class => ['setBlogPostSlug'],
        ];
    }

    /**
     * @param AfterEntityPersistedEvent $event
     * @param ChatterInterface $chatter
     * @return void
     * @throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
     */
    public function setBlogPostSlug(AfterEntityPersistedEvent $event)
    {
        $chatter = new Chatter();
        $entity = $event->getEntityInstance();

        if (!($entity instanceof Blog)) {
            return;
        }

        $message = (new ChatMessage('You got a new invoice for 15 EUR.'));
            // if not set explicitly, the message is send to the
            // default transport (the first one configured)
        $chatter->send($message);

    }
}

But when I run the code I have the followinf error :

App\EventSubscriber\EasyAdminSubscriber::setBlogPostSlug(): Argument #2 ($chatter) must be of type Symfony\Component\Notifier\ChatterInterface, string given

I don't understand what's wrong. Do you any idea why ? And how to make things work ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文