Symfony fosuserbundle在验证登录时获取错误消息

Symfony fosuserbundle在验证登录时获取错误消息,symfony,fosuserbundle,Symfony,Fosuserbundle,我使用以下命令覆盖登录操作: namespace PjDZ\UserBundle\Controller; use FOS\UserBundle\Controller\SecurityController as BaseController; use Symfony\Component\HttpFoundation\RedirectResponse; class SecurityController extends BaseController { public function lo

我使用以下命令覆盖登录操作:

namespace PjDZ\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SecurityController extends BaseController {
    public function loginAction(\Symfony\Component\HttpFoundation\Request $request)
    {
         $response   = parent::loginAction($request);

        //how can i get error message if exist from $response ??

        return $response;
    }
}
我想获取在主登录操作中生成的错误消息。
谢谢你,很抱歉我的英语不好。

错误消息存在于请求或会话中。如果要访问它,只需从以下位置复制代码:


注意,我删除了
$session->remove()
部分,因此父操作也能够从会话中获取错误消息。

哪个版本的FOSUserBundle?嗯,我不知道:p我从“friendsofsymfony/user bundle”下载了它:“dev master”希望可以帮助您。您真正需要做什么?也许新事件更好:在main loginAction()中,如果身份验证失败,我们将生成一条错误消息,我希望在覆盖loginAction()中使用此消息。当我尝试此解决方案并访问/login表单时,我有一个空白页。嗯,缩进一个php错误。查看webserver.Btw的error.log。我忘了添加
$session
变量。编辑了我的答案。清除cach后,我得到的错误是:ClassNotFoundException:试图从C:\wamp\www\PagesJaunesDZ\src\PjDZ\UserBundle\Controller\SecurityController.php第24行中的命名空间“PjDZ\UserBundle\Controller”加载类“SecurityContext”。你需要从另一个名称空间“使用”它吗?是的,很简单,复制后忘记了
use
语句,你应该自己修复它。你的家庭作业;)
namespace PjDZ\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\SecurityContext;

class SecurityController extends BaseController
{
    public function loginAction(\Symfony\Component\HttpFoundation\Request $request)
    {
        /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
        $session = $request->getSession();

        // get the error if any
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = '';
        }

        if ($error) {
            $error = $error->getMessage();
        }

        // do something with the $error message!

        return parent::loginAction($request);;
    }
}