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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
Php 覆盖Symfony 3.4中的默认FOSUserBundle控制器_Php_Symfony_Overriding_Fosuserbundle_Symfony 3.4 - Fatal编程技术网

Php 覆盖Symfony 3.4中的默认FOSUserBundle控制器

Php 覆盖Symfony 3.4中的默认FOSUserBundle控制器,php,symfony,overriding,fosuserbundle,symfony-3.4,Php,Symfony,Overriding,Fosuserbundle,Symfony 3.4,我正在使用Fosuserbundle来管理我的项目{SF::3.4.8}中的成员, 尝试按照Symfony文档覆盖registrationController的控制器时 <?php namespace TestUserBundle; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use FOSUserBundle\Controller

我正在使用Fosuserbundle来管理我的项目{SF::3.4.8}中的成员, 尝试按照Symfony文档覆盖registrationController的控制器时

<?php

namespace TestUserBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOSUserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController {

    public function registerAction(Request $request)
    {
        die("Hello");
    }
}

首先,覆盖控制器可能不是处理的最佳方式。你应该考虑进入控制器。以下是相关文档:

然后,如果仍要覆盖控制器,则应在依赖项注入中执行操作。控制器的服务名称为
fos\u user.registration.controller

要更换该服务,您只需使用:

services:
    fos_user.registration.controller: 
        class: YourController
        arguments:
            $eventDispatcher: '@event_dispatcher'
            $formFactory: '@fos_user.registration.form.factory'
            $userManager: '@fos_user.user_manager'
            $tokenStorage: 'security.token_storage'

您还可以在中覆盖它。这可能是最适合您的解决方案,因为您可以在另一个包中执行此操作

下面是它的外观:

使用Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
使用Symfony\Component\DependencyInjection\ContainerBuilder;
类ReplaceRegistrationController扩展CompilerPassInterface
{
公共函数进程(ContainerBuilder$container)
{
$container
->getDefinition('fos_user.registration.controller'))
->setClass(您的控制器::类)
;
}
}
别忘了:


您的捆绑包是
FOSUserBundle
的子捆绑包吗?您是否为该操作定义了与symfony文档中所述的fos\u用户注册路径相同的注册路径?是的,但我使用的是symfony 3.4,因此将忽略symfony文档中所述的“自symfony 3.4以来,捆绑包继承已被弃用”尝试使用cli命令调试路由,查看注册路径是否与自定义注册路由匹配您使用的是什么版本?“friendsofsymfony/user bundle”:“~2.0”,“symfony/symfony”:“3.4.*”,
$container->addCompilerPass(new ReplaceRegistrationController());