Search code examples
symfonyfosuserbundle

User Deprecated: The "fos_user.change_password.form.factory" service is private


I am getting following error. I am using symfony 3.4 and fos bundle. This error arose on password change route. Please if some can help quickly. I am in little bit hurry.

enter image description here


Solution

  • I assume you are trying to get the form factory in your controller like this:

    $this->get('fos_user.change_password.form.factory');
    

    In symfony, you can define services, which are classes that symfony will create when requested. They are usually defined in

    • config/services.yaml
    • config/services.xml
    • or config/services.php

    Services can have other services as their arguments, e.g. in yaml:

    serviceA:
        class: AppBundle\Service\ServiceA
        arguments:
            $email: 'test@domain.com'
    serviceB:
        class: AppBundle\Service\ServiceB
        arguments:
            $serviceA: '@serviceA'
    

    The @ symbol is important: that's what tells the container you want to pass the service whose id is serviceA and not just the string "serviceA"

    The services definition would be:

    class serviceA {
    
        public function __construct($email) {
    
        }
    }
    
    class serviceB {
    
        public function __construct(ServiceA $serviceA) {
    
        }
    }
    

    In this case, serviceA is a dependency for serviceB, meaning that symfony upon creating serviceB will also create serviceA(if it was not yet created) and provide it to serviceB `s constructor.

    Services are declared either as private or public: public vs private services.

    Since symfony 3.4, services are private by default: symfony 3.4 service changes

    You cannot access private services directly through the service container, you have to inject them to other services or your controllers(by defining your controllers as services).

    If you want to use it in your controller eg. MyController, first define your controller as a service:

    AppBundle\Controller\MyController:
        class: AppBundle\Controller\MyController
        public: true
        arguments:
            $formFactory: '@fos_user.change_password.form.factory'
    

    and change the controller to:

    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use FOS\UserBundle\Form\Factory\FormFactory;
    
    /**
     * @Route(service="AppBundle\Controller\MyController")
    */
    class MyController {
    
        private $changePasswordFormFactory;
        public function __construct(FormFactory $formFactory) {
             $this->changePasswordFormFactory = $formFactory;
        }
    }
    

    FOS user bundle change password service configuration

    Interestingly, the same was requested at the FOS user bundle repo, was merged but then it was reverted:

    Pull request Issue comment