FriendsOfSymfony / FOSUserBundle

Provides user management for your Symfony project. Compatible with Doctrine ORM & ODM, and custom storages.

Home Page:https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Service "fos_user.user_manager" not found

u1tr0n-ru opened this issue · comments

php: 7.2.4
symfony: 4.1.3
userBundle: 2.1.2
when write in any controller code:
$userManager = $this->get('fos_user.user_manager');
make an error:

Service "fos_user.user_manager" not found: even though it exists in the app's container, the container inside "App\Controller\AdminController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session", "templating" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "AdminController::getSubscribedServices()".

You should use dependency injection.

Update your service.yaml to inject the required service in your controller.

# config/services.yaml
App\Controller\UsersController:
        arguments:
            $fos_manager: '@fos_user.user_manager'

Then update your controller to use it

// src/Controller/UsersController.php
namespace App\Controller;

class UsersController {
    private $fos_manager;

    // constructor
    public function __construct($fos_manager)  {
        $this->fos_manager = $fos_manager;
    }

    public function xyz() {
        // update $user as you'd like and persist to DB
        $this->fos_manager->updateUser($user);
    }
}