0

I want to use service container inside my class so I have this this:

class SomeController extends ControllerBase {

  protected $entityTypeManager;
  protected $requestStack;

  public function __construct(EntityTypeManagerInterface $entityTypeManager,RequestStack $requestStack){
    $this->entityTypeManager = $entityTypeManager;
    $this->requestStack = $requestStack;
   }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('request_stack'),
    );
  }

  public function content() {
    $testEntityTypeManager = $this->entityTypeManager->getStorage('node')->getQuery();
    $testRequest = $requestStack->getCurrentRequest();
    //do something with these...
  }

}

What I wanted to do here is create a container for EntityTypeManager and RequestStack. Now phpStorm is giving me a warning regarding public static function create(ContainerInterface $container) and it says Method call uses 2 parameters, but method signature uses 1 parameters and I can't use it in my method content().

$testEntityTypeManager and $testRequest are just examples with which I would do something else but even now it doesn't work and phpStorm tells me getCurrentRequest().

Sure I can use them statically but that is not what I'm trying to achieve here. Any push in right direction is appreciated!

  • The method name should be __construct and not __constructor – Kevin Nov 18 '19 at 21:15
  • @kevin Thanks! PhpStorm still showing same warning. – Budala1 Nov 18 '19 at 21:34
  • You have a MYMODULE.services.yml? – leymannx Nov 18 '19 at 21:49
  • 2
    A controller receives the request as argument content(Request $request) and $entityTypeManager is already provided through the base class. It's unclear what you are trying to achieve, but if you want to have a clean controller class with proper DI then don't extend ControllerBase, only implement ContainerInjectionInterface. – 4k4 Nov 18 '19 at 22:17
  • 2
    OK, then don't start with this container factory thing, you rarely need this. Use the base class as it is or build a service and inject via mymodule.services.yml, which you can use as controller, too, see drupal.org/docs/8/api/routing-system/structure-of-routes – 4k4 Nov 18 '19 at 23:01
0

The error:

Method call uses 2 parameters, but method signature uses 1

Your create() method is defined to only receive a single argument, $container. This must be a debugger in PHPStorm giving you this output, because what it is saying is that the code calling SomeController::create() is passing two arguments. So the second is getting lost. Your problem is likely stemming from that, and probably means one of two things:

  1. The code calling the create() function is mistakenly passing a second value, or
  2. Your create() function needs to be re-defined to catch the second argument, and do something with it.

ControllerBase::create only expects a single argument, $container, and since your function defines that, I'd guess the more likely culprit is the calling function passing an unnecessary argument (#1 above). However, if the passed variable is necessary for some reason, SomeController::create() can be redefined to catch an optional argument:

public function create(ContainerInteface $container, $additional_param1 = NULL) {
  if (is_null($additional_param1)) {
    throw new \Exception('$additional_param_1 is required');
  }

  // Continue as normal.

}
  • It's also possible this persons code had no use statements. Since they are extending ControllerBase, I am not sure how the above scenario could happen unless there is custom code not mentioned overriding some core definition. – Kevin Nov 19 '19 at 2:10
  • Good addition. I’ll update my reply – Jaypan Nov 19 '19 at 2:23
  • @Jaypan Thanks! This works. I already had correct use statements. – Budala1 Nov 19 '19 at 8:39
  • If my answer fixed your problem, please mark it as the correct answer. Thank you. – Jaypan Nov 19 '19 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.