登录检查后在Symfony2中执行代码 [英] Execute a code after login check in Symfony2

查看:78
本文介绍了登录检查后在Symfony2中执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony2提供了一个非常简单的用户登录工具.实际上,该框架会为您执行所有安全控制.如官方文档所示,控制器( SecurityController )可用于加载登录表单页面.然后,将路由与用户通过登录表单提供的登录检查相关联.如果登录正确,则将用户重定向到主页(或另一个页面);否则,将用户重定向到主页.否则,用户必须重新输入用户名和密码.

Symfony2 provides a very simple tool for user login. In practice, the framework do all the security controls for you. As shown in the official doc, a controller (SecurityController) can be used to load the login form page. Then, a route is associated to login check the input provided by the user through the login form. If the login is correct, the user is redirected to the home page (or another one); otherwise, the user must re-enter username and password.

由于我需要在会话中从数据库中加载一些用户首选项,因此我想知道一旦登录检查确定了用户登录的正确性,是否有一种方法可以执行这种功能. 有什么主意吗?

Since I need to load in the session some user preference from a database, I want to know if there is a way to execute such a function once the login check ascertained the correctness of the user login. Any idea?

推荐答案

您必须编写自己的AuthenticationHandler,它将实现下面的两个接口:

You have to write your own AuthenticationHandler which will implement two interfaces below:

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface

在该处理程序中,您应该定义方法onAuthenticationSuccess()

In that handler you should define method onAuthenticationSuccess()

在此方法中,您可以执行所需的任何操作.

Inside this method you can do whatever you need.

也不要忘记在security.yml

firewalls:
    main:
        ....
        your_form_login:
            ...
            success_handler: your.authentication_handler

还要检查此有用的文档

这篇关于登录检查后在Symfony2中执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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