Symfony 2 - 在控制器之外设置 Flash 消息 [英] Symfony 2 - Setting a Flash Message outside of Controller

查看:21
本文介绍了Symfony 2 - 在控制器之外设置 Flash 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注销监听器,我想在其中设置显示注销确认消息的闪现消息.

I have a logout Listener where I'd like to set a flash message showing a logout confirmation message.

namespace Acme\MyBundle\Security\Listeners;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LogoutListener implements LogoutSuccessHandlerInterface
{
  private $security;  

  public function __construct(SecurityContext $security)
  {
    $this->security = $security;
  }

  public function onLogoutSuccess(Request $request)
  {
    $request->get('session')->getFlashBag()->add('notice', 'You have been successfully been logged out.');

    $response = new RedirectResponse('login');
    return $response;
  }
}

这是我的 services.yml(因为它与此有关):

Here is my services.yml (as it pertains to this):

logout_listener:
   class:  ACME\MyBundle\Security\Listeners\LogoutListener
   arguments: [@security.context]

这是一个错误:

Fatal error: Call to a member function getFlashBag() on a non-object

如何在这种情况下设置 flashBag 消息?

How do I set a flashBag message in this context?

另外,如何访问路由器以便我可以生成网址(通过 $this->router->generate('login'))而不是传入硬编码的网址?

Also, how do get access to the router so I can generate the url (via $this->router->generate('login')) instead of passing in a hard-coded url?

分辨率说明

为了让 flash 工作,你必须告诉你的 security.yml 配置不要在注销时使会话无效;否则,会话将被销毁,您的闪存将永远不会出现.

To get the flash to work, you must tell your security.yml config not invalidate the session on logout; otherwise, the session will be destroyed and your flash will never appear.

logout:
    path: /logout
        success_handler: logout_listener
        invalidate_session: false

推荐答案

您应该将 session 和 router 的服务注入 LogoutListener 并使用它们来执行这些任务.这是在 yml 中的做法:

You should inject the services for session and router into the LogoutListener and use them to perform these tasks. This is the way to do it in yml:

logout_listener: 
class: ACME\MyBundle\Security\Listeners\LogoutListener 
arguments: [@security.context, @router, @session]

然后在你的课堂上写:

class LogoutListener implements LogoutSuccessHandlerInterface
{
    private $security;
    private $router;
    private $session;

    public function __construct(SecurityContext $security, Router $router, Session $session)
    {
        $this->security = $security;
        $this->router = $router;
        $this->session = $session;
    }
    [...]

当您现在想使用会话时,您只需说:

When you want to use the session now you can just say:

$this->session->getFlashBag()->add('notice', 'You have been successfully been logged out.');

同样的,你可以使用路由服务来生成路由.

And in the same way you can use the router service to generate routes.

这篇关于Symfony 2 - 在控制器之外设置 Flash 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆