【问题标题】:Symfony Mgilet notification Bundle handle eventsSymfony Mgilet 通知 Bundle 处理事件
【发布时间】:2019-05-15 12:32:08
【问题描述】:

我没有很多 Symfony 经验,但我正在开发一个使用 https://github.com/maximilienGilet/notification-bundle 的 Symfony 4 应用程序 添加和显示通知运行良好,但我坚持处理捆绑包创建的事件。

树枝模板中的一个表单(实际上是在我的基本树枝模板中)正在调用 bundle post markasseen 操作,该操作返回 JSON 真实消息,但现在我不知道如何返回到其中的 URL(路由)邮局电话结束了吗?

这是我的 routers.yaml,它“激活”了捆绑控制器:

App\EventListener\NotificationListener:
    arguments: ['@router']
    tags:
        - { name: kernel.event_listener, event: mgilet.notification.seen }

如您所见,我尝试传递一个路由器参数,看看这是否让我有可能“返回”,但不幸的是,这并没有帮助。

这是我为处理捆绑事件而创建的 EventListener:

<?php

namespace App\EventListener;

use Mgilet\NotificationBundle\Event;
use Mgilet\NotificationBundle\Event\NotificationEvent;

use \Symfony\Bundle\FrameworkBundle\Routing\Router;
use \Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

class NotificationListener
{
    private $router;

    public function __construct(Router $router) {
        $this->router = $router;
    }

    public function OnMgiletNotificationSeen(Event\NotificationEvent $event): void {
        dd($event);
    }

}

正如我所提到的,事件被侦听器捕获,它返回“真”,但我想返回到调用发布操作的路由/控制器。

这是调用通知控制器的基本模板中的表单(因为我想在所有模板中显示通知):

<form action="/notifications/1/mark_as_seen/8" method="post">
    <button type="submit" class="close">
        <span aria-hidden="true">×</span><span class="sr-only">Close</span>
    </button>
</form>

谁能帮帮我?

【问题讨论】:

  • 你能在你实例化表单的地方添加控制器吗?
  • @NicolaPez 感谢您查看我的问题。我不确定你是什么意思?此表单由 twig 模板中的 Mglit 渲染函数“{{mgilet_notification_render}}”创建。
  • 好的,抱歉,我不明白表单完全是从捆绑包中创建的。您是否尝试在捆绑文档 github.com/maximilienGilet/notification-bundle/issues 上打开问题?
  • 因为我认为对于捆绑包中的特定问题来说是更好的地方
  • @NicolaPez 再次感谢您的评论。我会检查 Github,但是因为我认为从侦听器中的 json 响应返回会发生更多的事情?

标签: php symfony events event-listener


【解决方案1】:

正如https://stackoverflow.com/a/28913544/5475228 所说,您可以通过这种方式使用 twig 变量获得当前路线:

app.request.attributes.get('_route')

你也可以使用这个:

app.request.headers.get('referer')

所以,如果我理解您的问题,您可以在 json 响应为 true 后重定向到此路由。

【讨论】:

  • 但是如何从这个监听器调用重定向呢? $this->redirect 和 $event-redirect 或 $this->router->redirect 一样会产生错误吗?
  • 我认为问题对我来说不是那么清楚,但在我的解决方案中,您在前端(js?)中有路由,因为当我看到捆绑包时,我认为是前端行为。所以你可以用js进行重定向。
【解决方案2】:

我已经(暂时)解决了我的问题,但我认为这不是最好的解决方法。

我在 routes.yaml 中禁用了 bundle 控制器并创建了一个处理帖子的自定义控制器。

class NotifyController extends Controller
{
    /**
     * Set a Notification as seen
     * @Route("/notifications/{notifiable}/mark_as_seen/{notification}", name="notification_mark_as_seen", methods={"POST"})
     */
    public function markAsSeenAction($notifiable, $notification, Request $request)
    {
        $manager = $this->get('mgilet.notification');
        $manager->markAsSeen(
            $manager->getNotifiableInterface($manager->getNotifiableEntityById($notifiable)),
            $manager->getNotification($notification),
            true
        );

        return $this->redirect($request->headers->get('referer'));
    }

    /**
     * Set all Notifications for a User as seen
     * @Route("/notifications/{notifiable}/markAllAsSeen", name="notification_mark_all_as_seen", methods={"POST"})
     */
    public function markAllAsSeenAction($notifiable, Request $request)
    {
        $manager = $this->get('mgilet.notification');
        $manager->markAllAsSeen(
            $manager->getNotifiableInterface($manager->getNotifiableEntityById($notifiable)),
            true
        );

        return $this->redirect($request->headers->get('referer'));
    }
}

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2018-11-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多