我正在使用FSOUserBundle,我想覆盖loginAction,我使用了这个方法:
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,
));
}
}
但是当我尝试显示/登录页面时,我得到一个空白页面,任何想法?!! 请原谅我糟糕的英语。
答案 0 :(得分:7)
在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"
答案 1 :(得分:5)
我使用以下方法修正了错误:
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。以索引为例,像我一样。你会得到以下例外:
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 :(得分:4)
首先,你的包的父级是否设置为FOSUserBundle?
// Acme\UserBundle\AcmeUserBundle.php
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
其次,你清除了缓存吗?
答案 3 :(得分:0)
对于试图使用Symfony 4执行此操作的任何人,您只需要在routes.yaml中使用与要覆盖的路径相同的路径,并将其指向您自己的控制器。
fos_user_security_login:
path: /login
controller: App\Controller\SecurityController::loginAction