且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony 2 - 在控制器之外设置 Flash 消息

更新时间:2023-11-19 19:02:52

您应该将 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.