Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony fosuserbundle覆盖登录操作_Symfony_Fosuserbundle - Fatal编程技术网

Symfony fosuserbundle覆盖登录操作

Symfony fosuserbundle覆盖登录操作,symfony,fosuserbundle,Symfony,Fosuserbundle,我使用的是FSOUserBundle,我想覆盖loginAction,我使用了以下方法: namespace PjDZ\UserBundle\Controller; use FOS\UserBundle\Controller\SecurityController as BaseController; class SecurityController extends BaseController { public function loginAction(\Symfony\Component\

我使用的是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,
    ));
}
}
但当我试图显示/登录页面时,我得到了一个空白页面,你知道吗?!!。
请原谅我的英语不好。

首先,您的包的父包是否设置为FOSUserBundle

// Acme\UserBundle\AcmeUserBundle.php

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

其次,您是否已清除缓存?

我使用以下方法修复了错误:

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 

在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

"friendsofsymfony/user-bundle": "~2.0@dev"

对于任何试图使用Symfony 4执行此操作的人,只需在routes.yaml中使用与要覆盖的名称相同的路由,并将其指向您自己的控制器即可

fos_user_security_login:
    path: /login
    controller: App\Controller\SecurityController::loginAction

完全正确的答案,清除缓存是我忽略了做的事情,但为我解决了这个问题。服务:fos_user.security.controller:class:MAIN\MAINFOSUserBundle\controller\SecurityController参数:['@?security.csrf.token\u manager']它工作得更好4me