安装FOSUserBundle时出现Symfony 4.4错误

问题描述

我关注了这两篇有关“如何在symfony 4.4中安装fosuserbundle”的文章:

https://vfac.fr/blog/how-install-fosuserbundle-with-symfony-4

https://ourcodeworld.com/articles/read/794/how-to-install-and-configure-fosuserbundle-in-symfony-4

但是最后我得到了这个错误:

传递给FOS \ UserBundle \ Doctrine \ UserManager :: __ construct()的参数3必须是Doctrine \ Common \ Persistence \ ObjectManager的实例,Doctrine \ ORM \ EntityManager的实例 给定的名称,在第1466行的/url/to/symfony/proyect/var/cache/dev/ContainerKx7xY28/srcApp_KernelDevDebugContainer.php中调用

enter image description here

我没有更改有关FOSUserBundle的任何内容,但似乎我的配置有问题...

这些是我的配置文件:

security.yaml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        #users_in_memory: { memory: null }
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            #anonymous: lazy
            #provider: users_in_memory
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager

            logout:       true
            anonymous:    true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/login$,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting,role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/,role: ROLE_ADMIN }

packages / fos_user.yaml

# config/packages/fos_user.yaml
fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User
    from_email:
        address: "email@email.com"
        sender_name: "email@email.com"

src / Entity / User.php

<?php
// src/Entity/User.php

namespace App\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

编辑:我刚刚在symfony 4.3上尝试了相同的指南,并且有效!所以我认为与symfony 4.4和FOSUserBundle的兼容性有关。

解决方法

这与fos用户捆绑包无关,但与用户声明有关。

阅读this post first

请参阅您的用户声明:

<?php
// src/Entity/User.php

namespace App\Entity;

use Doctrine\Common\Persistence\ObjectManager; <---

尝试将其替换为

use Doctrine\ORM\EntityManagerInterface; <---

您可能在代码中的某个地方使用了用ObjectManager而不是EntityManagerInterface声明的实体对象。

如果这不起作用,请解释为什么添加此行:

// Añadimos esta linea porque parece que hacer algo...
use Doctrine\Common\Persistence\ObjectManager;

编辑

好吧,我一直在寻找它,这看起来像是一个学说中的错误。 我发现了这个问题:https://github.com/doctrine/orm/issues/8242

它可以解决您的问题。

只需像这样更新您的composer.json即可:

...
"require": {
        "php": ">=7.1.3","ext-ctype": "*","ext-iconv": "*","composer/package-versions-deprecated": "^1.11","doctrine/annotations": "^1.0","doctrine/doctrine-bundle": "^2.1","doctrine/doctrine-migrations-bundle": "^3.0","doctrine/orm": "^2.7","doctrine/common":"^2.13",<------
...

enter image description here

,

我在互联网上找到了很多找到最佳解决方案的方法,但没有一个解决了我的问题,所以我看到了使用捆绑包的服务并看到 fos_user.user_manager.default: service 是调用 {{1 }} 所以我用我自己的类重写它

Doctrine\Modle\UserManager
  1. 创建你自己的类来管理你的fos_user.user_manager.default: class: App\Model\UserManager arguments: - '@fos_user.util.password_updater' - '@fos_user.util.canonical_fields_updater' - '@doctrine.orm.entity_manager' - '%fos_user.model.user.class%' 实体,它必须扩展fosUser(可以复制同一个类的代码)
  2. 重写注入相同参数的服务,除了第三个参数外,将替换为FOS\UserBundle\Model\UserManager

希望能帮到你,它对我有用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...