1

I created a module for Craft 3 using pluginfactory.io, and am unable to call services from the primary module class using $this->orders->method() as shown in the documentation.

The services appear to be registered correctly, as I'm able to call the service methods elsewhere in the module using the SiteModule::$instance->orders->method() format.

The line use modules\sitemodule\services\Orders as OrdersService is at the top of the module class, but removing makes no difference in being able to call those service methods from the primary module class.

What is the correct way to call service methods from a primary module class?

2

Where do you instantiate your orders component?

As long as you're calling the setComponents early in your init method as follow, you should be able to access it anywhere in your plugin class:

...
parent::init();
self::$plugin = $this;
$this->setComponents([
        'orders' => OrdersService::class,
]);
...
  • Not sure how the services were getting registered before so that they worked in every other module file except the primary module class, but in any case this worked. Thanks for your help! – Joel Jun 25 '19 at 18:59

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.