fosuserbundle覆盖登录操作 [英] fosuserbundle override login action

查看:67
本文介绍了fosuserbundle覆盖登录操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FSOUserBundle,并且我想覆盖loginAction,我使用了以下方法:

I am using FSOUserBundle and I want to override loginAction, I used this method:

namespace PjDZ\UserBundle\Controller;
use FOS\UserBundle\Controller\SecurityController as BaseController;

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 (works with forward and redirect -- see below)
    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);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = '';
    }

    if ($error) {
        // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
        $error = $error->getMessage();
    }
    // last username entered by the user
    $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);

    $csrfToken = $this->container->has('form.csrf_provider')
        ? $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate')
        : null;


    return $this->renderLogin(array(
        'last_username' => $lastUsername,
        'error'         => $error,
        'csrf_token' => $csrfToken,
    ));
}
}

但是当我尝试显示/login页面时,我得到一个空白页面,知道吗?! 原谅我的英语不好.

but when I try to show /login page I get a blank page, any idea ?!!. forgive my bad english.

推荐答案

我通过使用以下方法解决了错误:

I fixed error by using:

namespace PjDZ\UserBundle\Controller;
use FOS\UserBundle\Controller\SecurityController as BaseController;

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

        //do something else;

        return $response;
     }
}

谢谢,这解决了我的问题. 确保您不使用其他Basecontroller.以索引为例,就像我一样.您将收到以下异常:

Thanks this fixed my problem. Be sure that you dont use another Basecontroller. For the index for example, like me. You would get following exception:

 Compile Error: Cannot use FOS\UserBundle\Controller\SecurityController as BaseController because the name is already in use 

我用过:

use FOS\UserBundle\Controller\SecurityController as FOSController;

class SecurityController extends FOSController 

这篇关于fosuserbundle覆盖登录操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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