问题描述
我正在使用fSouserBundle,我想覆盖登录,我使用了此方法:
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, )); } }
但是,当我尝试显示/登录页面时,我会得到一个空白页,任何想法?!!. 原谅我的英语不好.
推荐答案
我通过以下方式解决了错误:
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
其他推荐答案
在Symfony版本3.3和FosuserBundle 2.1.0中,上面的代码将不再起作用,直到添加 config.yml 中的行直至添加行
services: fos_user.security.controller: class: MAIN\MAINFOSUserBundle\Controller\SecurityController fos_user.resetting.controller: class: MAIN\MAINFOSUserBundle\Controller\ResettingController fos_user.profile.controller: class: MAIN\MAINFOSUserBundle\Controller\ProfileController fos_user.change_password.controller: class: MAIN\MAINFOSUserBundle\Controller\ChangePasswordController
这主要是因为我在 composer.json
中保留最新的朋友."friendsofsymfony/user-bundle": "~2.0@dev"
其他推荐答案
首先,您的捆绑包的父母设置为fosuserbundle?
// Acme\UserBundle\AcmeUserBundle.php class AcmeUserBundle extends Bundle { public function getParent() { return 'FOSUserBundle'; } }
其次,您清除了缓存吗?
问题描述
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, )); } }
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; } }
EDIT: 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
I used:
use FOS\UserBundle\Controller\SecurityController as FOSController; class SecurityController extends FOSController
其他推荐答案
In Symfony version 3.3 and Fosuserbundle version 2.1.0, the above code will not work anymore until adding lines in config.yml:
services: fos_user.security.controller: class: MAIN\MAINFOSUserBundle\Controller\SecurityController fos_user.resetting.controller: class: MAIN\MAINFOSUserBundle\Controller\ResettingController fos_user.profile.controller: class: MAIN\MAINFOSUserBundle\Controller\ProfileController fos_user.change_password.controller: class: MAIN\MAINFOSUserBundle\Controller\ChangePasswordController
This is mainly because I keep friendsofsymfony up to date in composer.json
"friendsofsymfony/user-bundle": "~2.0@dev"
其他推荐答案
First off, is your bundle's parent set to FOSUserBundle?
// Acme\UserBundle\AcmeUserBundle.php class AcmeUserBundle extends Bundle { public function getParent() { return 'FOSUserBundle'; } }
Secondly, have you cleared your cache?