2014-01-17 93 views
5

我使用FSOUserBundle,我想重写的LoginAction,我用这个方法:fosuserbundle覆盖登录操作

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, 
    )); 
} 
} 

但是当我尝试显示/登录页面我得到一个空白页,什么想法!? 。 原谅我的坏英语。

回答

6

我用固定的错误:

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; 
    } 
} 

编辑:谢谢你这个固定我的问题。 确保您不使用其他基本控制器。例如,像我这样的索引。你会得到以下异常:

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 
2

首先,您的套件的父设置为FOSUserBundle?

// Acme\UserBundle\AcmeUserBundle.php 

class AcmeUserBundle extends Bundle 
{ 
    public function getParent() 
    { 
    return 'FOSUserBundle'; 
    } 
} 

其次,你清除了缓存吗?

+0

完全有效的答案,清除缓存是我忽视做的事情,但为我解决了这个问题。 – Lewis

0

在Symfony的3.3版本和Fosuserbundle 2.1.0版本,上面的代码将不再工作,直到将线config.yml

这主要是因为我一直friendsofsymfony最新的composer.json

"friendsofsymfony/user-bundle": "[email protected]"