使用Symfony 5,我有一个非常简单的DataTransformer:
namespace App\Form\DataTransformer;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\DataTransformerInterface;
class UploadTransformer implements DataTransformerInterface
{
private $uploadDirectory;
public function __construct(ParameterBagInterface $parameters)
{
$this->uploadDirectory = $parameters->get('upload_directory');
}
...
我还创建了一种表格类型:
namespace App\Form\Type;
use App\Form\DataTransformer\UploadTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
class UploadType extends AbstractType
{
private $uploadTransformer;
public function __construct(UploadTransformer $uploadTransformer)
{
$this->uploadTransformer = $uploadTransformer;
}
...
但是此表格返回错误:
无法自动装配服务“ App \ Form \ Type \ UploadType”:方法“ __construct()”的参数“ $ uploadTransformer”引用类“ App \ Form \ DataTransformer \ UploadTransformer”,但不存在此类服务。
我不明白此错误,因为:
我知道我可以从services.yaml注入服务,但是通常不应该自动注入DataTransformer,如文档中所示吗?
我的services.yaml
:
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
我认为您的services.yaml
中缺少此内容>
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.