FOSUserBundle 安装时出现 Symfony 4.4 错误
Symfony 4.4 Error on FOSUserBundle Installation
我关注了这两个 post 关于“如何在 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
但最后我得到了这个错误:
Argument 3 passed to FOS\UserBundle\Doctrine\UserManager::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager
given, called in /url/to/symfony/proyect/var/cache/dev/ContainerKx7xY28/srcApp_KernelDevDebugContainer.php on line 1466
我没有更改 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 用户包相关的问题,而是与您的用户声明有关。
阅读。
查看您的用户声明:
<?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", <------
...
我在互联网上找到了很多最佳解决方案,但没有一个能解决我的问题,所以我查看了使用捆绑包的服务,发现 fos_user.user_manager.default
:服务是调用 Doctrine\Modle\UserManager
所以我用自己的 class
重写了它
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%'
- 创建您自己的 class 来管理您的
fosUser
实体,它必须扩展 FOS\UserBundle\Model\UserManager
(可以复制相同 class 的代码)
- 重写注入相同参数的服务,除了第三个参数,它将被替换为
EntityManagerInterface
希望对你有所帮助,对我有用。
我关注了这两个 post 关于“如何在 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
但最后我得到了这个错误:
Argument 3 passed to FOS\UserBundle\Doctrine\UserManager::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager given, called in /url/to/symfony/proyect/var/cache/dev/ContainerKx7xY28/srcApp_KernelDevDebugContainer.php on line 1466
我的配置文件是这些:
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 用户包相关的问题,而是与您的用户声明有关。
阅读
查看您的用户声明:
<?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", <------
...
我在互联网上找到了很多最佳解决方案,但没有一个能解决我的问题,所以我查看了使用捆绑包的服务,发现 fos_user.user_manager.default
:服务是调用 Doctrine\Modle\UserManager
所以我用自己的 class
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%'
- 创建您自己的 class 来管理您的
fosUser
实体,它必须扩展FOS\UserBundle\Model\UserManager
(可以复制相同 class 的代码) - 重写注入相同参数的服务,除了第三个参数,它将被替换为
EntityManagerInterface
希望对你有所帮助,对我有用。