Reputation: 103
In my Symfony 4 project I've created a HelloBlock class in the /src/Blocks/Hello/HelloBlock.php file.
Here's its constructor...
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
And then in my services.yaml I've added this...
App\Blocks\:
resource: '../src/Blocks'
tags: ['controller.service_arguments']
When running my code (dev environment, cache cleared, etc) I'm getting the "Too few arguments" error. It's not injecting the dependency.
Can anyone help? I thought this is what the Symfony DI is supposed to do.
Thanks!
Upvotes: 3
Views: 2145
Reputation: 9845
You probably have to provide the arguments:
to the service definition.
App\Blocks\:
resource: '../src/Blocks'
tags: ['controller.service_arguments']
arguments:
- '@doctrine.orm.default_entity_manager'
@
is used to not interpret the name as a simple string but to fetch the actual service instead.
The naming is a little bit tricky; this answer made me understand how the naming is built: <entitymanager_name> (according to the doctrine.orm.entity_managers
YAML definition) concatenated to _entity_manager
.
With the special case of the default
one that is available as doctrine.orm.default_entity_manager
even when not explicitly defined in the above mentioned config key.
I tried on my app to just add that string as an argument and it didn't fail. Then I put a typo, and it failed. So I assume that default_entity_manager
is defined automatically (I am not sure where).
In case it doesn't work, the other fix would be to verify why the entityManager is not automatically wired. Check your config for autowiring the src/
folders.
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Tests, ....}'
and ensure that Blocks
is not listed among the exclude
folders.
Upvotes: 1