3

我关注了这两个关于“如何在 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 实例,在 /url/to/symfony/proyect/var 中调用/cache/dev/ContainerKx7xY28/srcApp_KernelDevDebugContainer.php 第 1466 行

在此处输入图像描述 我没有更改有关 FOSUserBundle 的任何内容,但我的配置似乎有问题...

我的配置文件是这些:

安全.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 }

包/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 的兼容性...

4

2 回答 2

1

这不是与 fos 用户捆绑包有关的问题,而是与您的用户声明有关。

先阅读这篇文章

查看您的用户声明:

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

在此处输入图像描述

于 2020-09-30T11:24:55.177 回答
1

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

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%'
  1. 创建您自己的类来管理您的fosUser实体,它必须扩展FOS\UserBundle\Model\UserManager(可以复制同一类的代码)
  2. 重写注入相同参数的服务,除第三个参数外,将替换为EntityManagerInterface

我希望这会对你有所帮助,它对我有用。

于 2021-01-17T08:18:20.540 回答