覆盖Symfony 3.4中的默认FOSUserBundle控制器 [英] Overriding Default FOSUserBundle Controller in Symfony 3.4

查看:121
本文介绍了覆盖Symfony 3.4中的默认FOSUserBundle控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fosuserbundle来管理项目{ SF :: 3.4.8 }中的成员, 通过遵循Symfony文档

I'm using the Fosuserbundle to manager members in my project { SF::3.4.8 }, when trying to override the controller of the registrationController by following the Symfony documentation

<?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");
    }
}

但是系统会忽略该控制器,但仍使用原始控制器,因此,如果有任何方法可以覆盖我的控制器,则

but the system ignore that controller and still use The original controller, so if there any way to override my controller by

推荐答案

首先,覆盖控制器可能不是最好的处理方式.您应该考虑将钩住到控制器中.以下是相关文档: https://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

First, overriding the controller is probably not the best way to process. You should consider to hook into controller. Here is the related documentation: https://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

然后,如果您仍然想覆盖控制器,则应该执行依赖项注入.控制器的服务名称为fos_user.registration.controller.

Then if you still want to override the controller, you should act in the dependency injection. The service name of the controller is fos_user.registration.controller.

要替换服务,您可以简单地使用:

To replace the service you can simply use:

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'


您还可以在 CompilerPass 中覆盖它.这可能是最适合您的解决方案,因为您需要在另一个捆绑包中进行此操作.


You can also override it in a CompilerPass. Which is probably the best solution for you because you do this inside another bundle.

这是它的外观:

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ReplaceRegistrationController extends CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $container
            ->getDefinition('fos_user.registration.controller')
            ->setClass(YourController::class)
        ;
    }
}

别忘了将其注册到您的捆绑包中:

$container->addCompilerPass(new ReplaceRegistrationController());

这篇关于覆盖Symfony 3.4中的默认FOSUserBundle控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆