【发布时间】:2018-05-27 07:55:49
【问题描述】:
我正在使用 symfony 3.4.1 和 doctrine/doctrine-fixtures-bundle": "2.4.1
我正在尝试运行固定装置,但是当我尝试注入 UserPasswordEncoderInterface 以对我的密码进行编码时出现此消息。
https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#accessing-services-from-the-fixtures
致命错误:未捕获 Symfony\Component\Debug\Exception\FatalThrowableError:类型错误:太 函数的几个参数 AppBundle\DataFixtures\DataFixtures::__construct(), 0 传入 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php 在第 210 行,正好 1 预计在 /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php:20 堆栈跟踪:
0 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(210):
AppBundle\DataFixtures\DataFixtures->__construct()
1 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(390):
Doctrine\Common\DataFixtures\Loader->createFixture('AppBundle\DataF...')
2 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(82):
Doctrine\Common\DataFixtures\Loader->loadFromIterator(Object(RecursiveIteratorIterator))
3 /srv/api-platform/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php(102):
Doctrine\Commo in /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php 上 第 20 行
我的赛程:
<?php
namespace AppBundle\DataFixtures;
use AppBundle\Entity\TechnicalCenter;
use AppBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class DataFixtures extends Fixture
{
/** @var UserPasswordEncoderInterface $encoder */
private $encoder;
/**
* DataFixtures constructor.
* @param UserPasswordEncoderInterface $encoder
*/
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
/**
* Load data fixtures with the passed EntityManager
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
$technicalCenter = new TechnicalCenter();
$technicalCenter->setName('Austerlitz');
$manager->persist($technicalCenter);
$admin = new User();
$admin->setUsername('admin');
$admin->setPassword($this->encoder->encodePassword($admin, 'admin'));
$admin->setRoles(array('ROLE_ADMIN'));
$admin->setTechnicalCenter($technicalCenter);
$manager->persist($admin);
$manager->flush();
}
}
我的security.yml
security:
encoders:
AppBundle\Entity\User: bcrypt
【问题讨论】:
-
您是否使用标准服务配置,即您的 services.yml 文件位于 Symfony 3.4 的标准位置?
-
如果不是(或者即使是),您是否尝试将您的设备注册为服务并注入编码器?