Cannot autowire service FOSUserBundle, Symfony 3.4
Asked Answered
E

2

5

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:

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".

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;
        }
     }

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.

Egidio answered 23/5, 2018 at 11:29 Comment(2)
Check symfony.com/blog/…Edaedacious
Adding an alias to your services.yaml file (like the error suggests) might help. Problem is that other FOS controllers (profile, change password etc) need different form factories. So you will need to manually wire the dependency. Check the autowire section in the docs. And as previously pointed out, bundle inheritance is going away so unless you plan on staying with 3.4 indefinitely then don't use it.Kali
K
6

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'

And you should be good to go.

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.

Kali answered 23/5, 2018 at 18:15 Comment(6)
I need the controller to give back two forms instead of one ( the register form ). I've never tried the event subscriber. Does it work in my case?Egidio
Nope. No idea why you might think that you need two forms to register but if you do then copy/paste away. Or consider just writing your own user management code. The FOSUserBundle is popular but does not lend itself to customizations such as yours.Kali
The workflow of the application needs to be: User makes a coffee preference > User makes an account Then an account gets created with a coffee preference. The only problem is that a coffee preference needs to be linked to an user. If I can make a custom register controller I can make both the coffee preference and the user at the same time. I hope this clarifies it.Egidio
You should be able to customize your registration form to add the coffee preference. Then listen for REGISTRATION_INITIALIZE and verify the user selected the preference. Then listen for REGISTRATION_SUCCESS and add the coffee link.Kali
Not sure exactly how this works too be honest but it works. Thanks!Egidio
Sorry for asking but you were right. I need an event listener that can post the coffee options after a user is registered. I've followed the docs and the listener works. The only problem is that it is called at the same time as the page renderer. I can't find out how I can call my listener after a user is registerd.Egidio
S
1

I know for sure, that this works for Symfony 3.2.3 This is only for symfony version <= 3. Because bundle inheritence is depraceted and will be disabled in 4. Change the version of FOSUserBundle in the composer.json to

"friendsofsymfony/user-bundle": "2.0.2"

and run command to update it

composer update
Schizoid answered 23/5, 2018 at 13:44 Comment(10)
And that will fix the problem in the question how? Have you tested this?Kali
read the comments after answer #50288063Schizoid
But this question specifies Symfony 3.4. I don't see how rolling back to an incompatible version of the user bundle is going to help.Kali
yea, in the version 2.0.2 of fosuser bundle there are no constructor in RegistrationController, instead all needed services are gotten from container in the register actionSchizoid
are you sure they are incompatible?Schizoid
Yep though I'm a bit surprised you asked. In general, when answering a question it's best to be sure it works before sending people down dead ends.Kali
i use it in symfony 3.2.3Schizoid
Let us continue this discussion in chat.Schizoid
Check whether your fosuserbundle was updated. There should not be any constructor In registerControllerSchizoid
it should be like that github.com/FriendsOfSymfony/FOSUserBundle/blob/2.0.x/Controller/…Schizoid

© 2022 - 2024 — McMap. All rights reserved.