不能自动布线服务[英] Cannot autowire service

本文是小编为大家收集整理的关于不能自动布线服务的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从fosuserbundle(Symfony3.4)实现Usermanager.

服务/register.php

<?php

namespace AppBundle\Service;

use FOS\UserBundle\Model\UserManager;


class Register
{
    private $userManager;

    public function __construct(UserManager $userManager)
    {
        $this->userManager = $userManager;
    }

    public function register() {
        $user = $this->userManager->findUserByUsernameOrEmail('aaa@gmail.clom');
        if($user){
            return false;
        }
        return true;
    }
}

当我尝试调用此方法时,我会得到:

无法自动服务" AppBundle \ Service \ register":参数" $ USERMANAGER"方法" __construct()" References类" FOS \ userBundle \ userBundle \ userbundle \ model \ usermanager",但不存在此类服务.您应该将此类别列为现有的" fos_user.user_manager.default"服务.

我现在该怎么办?

推荐答案

我也有类似的问题(在Symfony 4中,但原理应适用于3.4),并设法在Symfony Doc Page上找到答案自动定义服务依赖项(自动).

这是该页面的摘录:

配置自动功能的主要方法是创建一个服务,其ID与该类别完全匹配.在上一个示例中,服务的ID为AppBundle\Util\Rot13Transformer,它允许我们自动自动自动自动.

这也可以使用别名来完成.

您需要一个别名,因为服务ID与className不符.这样做:

# app/config/services.yml
services:
    # ...

    # the `fos_user.user_manager.default` service will be injected when
    # a `FOS\UserBundle\Model\UserManager` type-hint is detected
    FOS\UserBundle\Model\UserManager: '@fos_user.user_manager.default'

本文地址:https://www.itbaoku.cn/post/2090807.html

问题描述

I am trying to implement UserManager from FOSUserBundle (Symfony3.4).

Service/Register.php

<?php

namespace AppBundle\Service;

use FOS\UserBundle\Model\UserManager;


class Register
{
    private $userManager;

    public function __construct(UserManager $userManager)
    {
        $this->userManager = $userManager;
    }

    public function register() {
        $user = $this->userManager->findUserByUsernameOrEmail('aaa@gmail.clom');
        if($user){
            return false;
        }
        return true;
    }
}

When I try call this method I get:

Cannot autowire service "AppBundle\Service\Register": argument "$userManager" of method "__construct()" references class "FOS\UserBundle\Model\UserManager" but no such service exists. You should maybe alias this class to the existing "fos_user.user_manager.default" service.

What should I do now?

推荐答案

I had a similar problem (in Symfony 4, but the principles should apply to 3.4) with a different service and managed to find the answer on the Symfony doc page Defining Services Dependencies Automatically (Autowiring).

Here's an extract from that page:

The main way to configure autowiring is to create a service whose id exactly matches its class. In the previous example, the service's id is AppBundle\Util\Rot13Transformer, which allows us to autowire this type automatically.

This can also be accomplished using an alias.

You need an alias because the service ID doesn't match the classname. So do this:

# app/config/services.yml
services:
    # ...

    # the `fos_user.user_manager.default` service will be injected when
    # a `FOS\UserBundle\Model\UserManager` type-hint is detected
    FOS\UserBundle\Model\UserManager: '@fos_user.user_manager.default'