使用fosuser覆盖注册控制器无法自动装配服务" App \ Controller \ RegistrationController"

时间:2018-02-02 21:05:50

标签: symfony fosuserbundle symfony4

我正在尝试覆盖FosUserBundle注册表控制器,但会出现以下错误:

Cannot autowire service "App\Controller\RegistrationController": argument "$formFactory" of method "FOS\UserBundle\Controller\RegistrationController::__construct()" references interface "FOS\UserBundle\Form\Factory\FactoryInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "fos_user.profile.form.factory", "fos_user.registration.form.factory", "fos_user.change_password.form.factory", "fos_user.resetting.form.factory". Did you create a class that implements this interface?

我使用的是Symfony4,这是我的RegistrationController.php代码。 我尝试了多种方式,但我找不到办法让它发挥作用。

class RegistrationController extends BaseController
        {

            public function registerAction(Request $request)
            {

                $form                = $this->get('fos_user.registration.form.factory');
                $formHandler         = $this->get('fos_user.registration.form.handler');
                $confirmationEnabled = $this->getParameter('fos_user.registration.confirmation.enabled');

                die("Hello");

                $process = $formHandler->process($confirmationEnabled);
                if ($process) {
                    $user = $form->getData();

                    if ($confirmationEnabled) {
                        $this->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                        $route = 'fos_user_registration_check_email';
                    } else {
                       // $this->authenticateUser($user);
                        $route = 'users_edit';
                    }

                    $this->setFlash('fos_user_success', 'registration.flash.user_created');
                    $url = $this->get('router')->generate($route, array("id" => $user->id));

                    return new RedirectResponse($url);
                }

                return $this->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
                    'form' => $form->createView()
                ));
            }


        }
    ?>

10 个答案:

答案 0 :(得分:1)

我在SF4和FOSUserBundle上遇到了同样的问题。环顾了半天后,我提出了解决方案。我使用了@thibaut的一些材料,并使其适应SF 4.2。

我注入了fos用户注册控件所需的服务,并为FOS \ UserBundle \ Form \ Factory \ FactoryInterface赋予了别名

config / services.yaml

   app.controller.register:
    class: App\Controller\bundles\FOSUserBundle\RegistrationController
    arguments:
        $eventDispatcher: '@event_dispatcher'
        $formFactory: '@fos_user.registration.form.factory'
        $userManager: '@fos_user.user_manager'
        $tokenStorage: 'security.token_storage'
    calls:
        - method: setContainer
          arguments:
              - '@service_container'
    public: true   

FOS\UserBundle\Form\Factory\FactoryInterface:
    alias: 'fos_user.registration.form.factory' 
    public: true  

然后在我的注册控制器中,我重新声明一个构造函数并保存要在控制器中使用的formfactory

App \ Controller \ bundles \ FOSUserBundle

protected $formFactory;

public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage, $serviceContainer=null)
{
    $this->setContainer($serviceContainer);
    $this->formFactory = $formFactory;

    parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);
}

在我的操作中使用声明$ formfactory

/**
 * @Route("/register", name="registration")
 */
public function registerAction(Request $request)
{


    /** @var $formFactory FactoryInterface */
    $formFactory = $this->formFactory;
    /** @var $userManager UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

希望它对您也有帮助

答案 1 :(得分:0)

我不知道您使用的是FOSUserBundle的哪个版本,但尚未获得sf4的正式支持 - 请参阅release notes。您可以尝试使用dev-master版本并按照this issue进行操作。

答案 2 :(得分:0)

您应该使用挂钩并避免FOSUserBundel v2.x中的覆盖控制器 您可以在页面上阅读更多信息:http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

答案 3 :(得分:0)

我必须使用拒绝在config/services.yaml

中自动装配的FOS服务的别名

FOS\UserBundle\Form\Factory\FormFactory: '@fos_user.registration.form.factory'

vendor/friendsofsymfony/user-bundle/Resources/config/registration.xml

所示

答案 4 :(得分:0)

你好我想我找到了解决这个问题的方法:  定义您的服务:

app.controller.register:
    class: AppBundle\Controller\RegisterController
    arguments: ['@service_container']  
    public: true  

然后你的控制器

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

/**
 * @Route(service="app.controller.register")
 */
class RegisterController extends BaseController
{

    public function __construct($serviceContainer=null)
    {
        $this->setContainer($serviceContainer);
        $eventDispatcher=$this->container->get('event_dispatcher');
        $formFactory=$this->container->get('fos_user.registration.form.factory');
        $userManager=$this->container->get('fos_user.user_manager');
        $tokenStorage=$this->container->get('security.token_storage');

        parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);
    }

    /**
     * @Route("/register", name="register")
     */
    public function registerAction(Request $request)
    {
        //do your stuff
    }
}

希望这有帮助(抱歉英语不好)

答案 5 :(得分:0)

要在替代中解决此问题:

  • 完全覆盖FOSUserBundle RegistrationController,即将其全部内容复制到AppBundle / Controller / RegistrationController.php
  • 然后将__construct函数替换为以下内容:

    public function __construct() {
        $this->eventDispatcher = $this->get('event_dispatcher');
        $this->formFactory = $this->get('fos_user.registration.form.factory');
        $this->userManager = $this->get('fos_user.user_manager');
        $this->tokenStorage = $this->get('security.token_storage'); }
    

Voila!您只是在构造函数中调用这些服务,而不是将它们作为参数传递给注册控制器服务。

我现在这样做了,它适用于sf3.4。

答案 6 :(得分:0)

我在SF4中遇到了同样的问题,FOSUserBundle(在SonataAdmin后面)。 我想听一个事件:注册已确认,向管理员发送消息。 我的第一次尝试:覆盖FosUser控制器。但是问题很多! 感谢@majne,我去了http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html,并且即使我不在'master'中,也可以通过FOS \ UserBundle \ FOSUserEvents类将事件挂起。 我遇到了一些问题,需要明确修改使用声明...

答案 7 :(得分:0)

在symfony 4.2中工作

services.yaml

use FOS\UserBundle\Controller\RegistrationController as Base;

class RegistrationController extends Base
{

    private $eventDispatcher;
    private $formFactory;
    private $userManager;
    private $tokenStorage;

    public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage)
    {
        parent::__construct($eventDispatcher, $formFactory,$userManager, $tokenStorage);
        $this->eventDispatcher = $eventDispatcher;
        $this->formFactory = $formFactory;
        $this->userManager = $userManager;
        $this->tokenStorage = $tokenStorage;
    }

和控制器

cleos -u https://api.kylin.alohaeos.com push action suniltestacc hi '["bobmarley"]' -p doslnjslongd@active

答案 8 :(得分:0)

使用Symfony 4.2,以下方法应该起作用。

与其他答案中所建议的相反,无需扩展相应的FOSUserBundle控制器或修改__construct方法。

/config/services.yaml

FOS\UserBundle\Form\Factory\FactoryInterface: '@fos_user.registration.form.factory' 

在我的情况下,我实际上覆盖了RegistrationControllerResettingController,在这种情况下,需要同时连接两种形式-以及MailerInterface-

/config/services.yaml

FOS\UserBundle\Form\Factory\FactoryInterface:   '@fos_user.resetting.form.factory' 

FOS\UserBundle\Mailer\MailerInterface: '@fos_user.mailer.default' 

App\Controller\RegistrationController:
    autowire: true
    arguments: 
        $formFactory: '@fos_user.registration.form.factory'

答案 9 :(得分:-1)

我有同样的问题,我通过制作&#34; fos_user.resetting.form.factory&#34;来解决它。公共

#fos_user.yaml 
services:
    new_service_name:
        alias: fos_user.registration.form.factory
        public: true

然后使用具有新名称的服务

$form = $this->get('new_service_name');