I am developing a bundle JohnCorpFormsBundle
for my own symfony projects to share extended form logic.
However, when implementing the bundle in a project myproject
I always get an error when I load a page in myproject
that relies on bundle logic.
Error Message:
Too few arguments to function
JohnCorp\FormsBundle\Form\Builder\Core\ListFilterType::__construct()
,
0 passed in/var/www/myproject/vendor/symfony/form/FormRegistry.php
online 81
and exactly 1 expected
It seems, the parameter $requestStack
is not being passsed to ListFilterType::__construct()
but shouldn't this work automatically using autowire?
MyProject: Controller-Code that causes the error:
// UserListController.php
use JohnCorps\FormsBundle\Form\Builder\Core\ListFilterType;
// ...
// this line runs into the error:
$listFilterForm = $this->createForm(ListFilterType::class);
Bundle: Form class:
// ListFilterType::class
namespace JohnCorp\FormsBundle\Form\Builder\Core;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\HttpFoundation\RequestStack;
class ListFilterType extends AbstractType
{
protected RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
//...
Bundle: Extension
// src/DependencyInjection/JohnCorpFormsExtension.php
namespace JohnCorp\FormsBundle\DependencyInjection;
class JohnCorpFormsExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');
}
}
Bundle: composer.json autoload
{
"name": "johncorp/forms-bundle",
"type": "symfony-bundle",
...
"autoload": {
"psr-4": {
"JohnCorp\\FormsBundle\\": "src/"
}
}
}
I tried adding the arguments explicitly in the bundles services.yaml
to ensure autowire is not a problem, but that does not change a thing.
Bundle: src/Resources/config/services.yaml:
services:
JohnCorp\FormsBundle\Form\Builder\Core\ListFilterType:
arguments:
$requestStack: '@request_stack'
I am not sure if this is required or if it is loaded. (I include this in a src/DependencyInjection/JohnCorpFormsExtension.php
)
I also tried adding the yaml
-Code from above directly inside the projects services.yaml
without any effect.
I also tried explicitly using autowire-Attribute in ListFilterType.php
// ListFilterType::class
public function __construct(
#[Autowire(service: 'request_stack')] RequestStack $requestStack
)
CodePudding user response:
If you use Symfony 6 , you just have to set the autowire and autoconfigure as true in your Bundle like this :
# yourBundle/src/Ressources/config/services.yaml
services:
JohnCorp\FormsBundle\Form\Builder\Core\ListFilterType:
autowire: true
autoconfigure: true
here is the doc : service-container-services-load-example
then after that you have to inject your service in your BundleExtension:
<?php
//yourBundle/src/DepencencyInjection/YourBundleExtension
namespace App\YourBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class YourBundleExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');
}
...
}