Constructor method inherited from another class can't see my additional arguments

142 Views Asked by At

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?

1

There are 1 best solutions below

0
Peter On

Instead of redefining the controller __construct method, you can inject the needed services directly to controller action method. For instance:

public function someAction(
    Sender $sender,
    ChannelContextInterface $channelContext, 
    LocaleContextInterface $localeContext
) {
    // method logic that uses services
}

In this case you:

  1. Don't need to overwrite the controller construct method and list all 17 parameters
  2. Don't need to define the controller class arguments in services.yaml file
  3. Inject only services needed in a specific controller method which is a bit more optimal

More info about injecting services in Symfony controller method can be found in Symfony Docs.