无法自动装配服务FOSUserBundle,Symfony 3.4 [英] Cannot autowire service FOSUserBundle, Symfony 3.4

查看:89
本文介绍了无法自动装配服务FOSUserBundle,Symfony 3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖FOSUserBundle的注册控制器. 我已经按照 https://symfony.com/doc/3.4/bundles上的步骤进行操作/inheritance.html 但出现以下错误:

I'm trying to override the registration controller of my FOSUserBundle. I've followed the steps on https://symfony.com/doc/3.4/bundles/inheritance.html But I get the following error:

无法自动装配服务"AppBundle \ Controller \ RegistrationController":方法"FOS \ UserBundle \ Controller \ RegistrationController :: __ construct()"的参数"$ formFactory"引用接口"FOS \ UserBundle \ Form \ Factory \ FactoryInterface",但没有这样的服务存在.您可能应该将此接口作为以下现有服务之一的别名:"fos_user.profile.form.factory","fos_user.registration.form.factory","fos_user.change_password.form.factory","fos_user.resetting.form".工厂".

Cannot autowire service "AppBundle\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".

我的RegistrationController.php:

My RegistrationController.php :

// src/UserBundle/Controller/RegistrationController.php
namespace AppBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;

    class RegistrationController extends BaseController
    {
        public function registerAction(Request $request)
        {
            $response = parent::registerAction($request);

            // ... do custom stuff
            return $response;
        }
     }

我的AppBundle.php

My AppBundle.php

<?php

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

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

如果需要更多信息,请告诉我,以便我更新此问题.

If more information is needed tell me so I can update this question.

推荐答案

我安装并配置了Symfony 3.4的新副本以及最新的FOSUserBundle 2.1

I installed and configured a fresh copy of Symfony 3.4 along with the latest FOSUserBundle 2.1

由于捆绑继承已消失,只需调整寄存器路由以指向您的控制器即可:

Since bundle inheritance is going away, just adjust the register route to point to your controller:

# config/routes.yaml
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

fos_user_registration_register:
    path: /register
    controller: AppBundle\Controller\RegistrationController::registerAction

然后将表单工厂注入到您的控制器中:

And then inject the form factory into your controller:

# app/services.yaml, keep all the standard defaults above
AppBundle\Controller\RegistrationController:
    arguments:
        $formFactory: '@fos_user.registration.form.factory'

你应该很好.

唯一剩下的问题是,为什么您首先要这样做?基本上,您将需要从基类复制/粘贴整个registerAction.大多数时候,您会想要创建一个 FOS事件订阅者并进行监听REGISTRATION_INITIALIZE,REGISTRATION_SUCCESS,REGISTRATION_COMPLETED或REGISTRATION_FAILURE事件.

The only remaining question is why you would want to do this in the first place? You would basically need to copy/paste the entire registerAction from your base class. Most of the time you would want to create an FOS event subscriber and listen for REGISTRATION_INITIALIZE, REGISTRATION_SUCCESS,REGISTRATION_COMPLETED or REGISTRATION_FAILURE events.

这篇关于无法自动装配服务FOSUserBundle,Symfony 3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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