I am writing a web application in PHP in the Sylius framework. I created a new class that inherits from the framework's built-in ResourceController class:
class ResourceController extends \Sylius\Bundle\ResourceBundle\Controller\ResourceController
The class \Sylius\Bundle\...\ResourceController has a constructor method with 17 arguments in it. In my class, which inherits that class, I need to add three arguments to the constructor. So my code looks like this:
public Sender $sender;
public ChannelContextInterface $channelContext;
public LocaleContextInterface $localeContext;
public function __construct(MetadataInterface $metadata, RequestConfigurationFactoryInterface $requestConfigurationFactory, ?ViewHandlerInterface $viewHandler, RepositoryInterface $repository, FactoryInterface $factory, NewResourceFactoryInterface $newResourceFactory, ObjectManager $manager, SingleResourceProviderInterface $singleResourceProvider, ResourcesCollectionProviderInterface $resourcesFinder, ResourceFormFactoryInterface $resourceFormFactory, RedirectHandlerInterface $redirectHandler, FlashHelperInterface $flashHelper, AuthorizationCheckerInterface $authorizationChecker, EventDispatcherInterface $eventDispatcher, ?StateMachineInterface $stateMachine, ResourceUpdateHandlerInterface $resourceUpdateHandler, ResourceDeleteHandlerInterface $resourceDeleteHandler, Sender $sender, ChannelContextInterface $channelContext, LocaleContextInterface $localeContext)
{
parent::__construct($metadata, $requestConfigurationFactory, $viewHandler, $repository, $factory, $newResourceFactory, $manager, $singleResourceProvider, $resourcesFinder, $resourceFormFactory, $redirectHandler, $flashHelper, $authorizationChecker, $eventDispatcher, $stateMachine, $resourceUpdateHandler, $resourceDeleteHandler);
$this->sender = $sender;
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
}
After launching the application, I got an (Symfony) error, even though theoretically I didn't skip anything (although I'm probably wrong)
Too few arguments to function App\Controller\Resource\ResourceController::__construct(), 17 passed in [...] on line 31 and exactly 20 expected
Fragment of _sylius.yaml file:
sylius_customer:
resources:
customer:
classes:
model: App\Entity\Customer\Customer
repository: App\Repository\CustomerRepository
controller: App\Controller\Resource\ResourceController
And config/routes.yaml file:
sylius_admin_customer_create:
path: /admin/customers/new
methods: [GET,POST]
defaults:
_controller: sylius.controller.customer::createAction
What am I doing wrong?
Instead of redefining the controller
__constructmethod, you can inject the needed services directly to controller action method. For instance:In this case you:
services.yamlfileMore info about injecting services in Symfony controller method can be found in Symfony Docs.