【发布时间】: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