Answers.Body: <p>You need to call your <code>setComponents</code> function before you are trying to access your components otherwise - logically - they are not defined</p>
  
  <pre><code>public function init()
  {
      parent::init();
      self::$plugin = $this;
  
      // define the components / services
      $this-&gt;setComponents(
          [
              'serviceExample' =&gt; ServiceExample::class
          ];
      );
  
      // access them
      $this-&gt;serviceExample-&gt;serviceMethod();
      // if it's easier for you to remember you can as well use the longer/slower syntax
      AbandonedCart::$plugin-&gt;serviceExample-&gt;serviceMethod();
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-19T09:10:58.617Z
Answers.Text: Thanks for the tip, this is indeed a better version of what I used. I had to use `services\ServiceExample::class` to get it working with my services. This however doesn't work for actions in my controller. I get the following error: `Missing required parameter "id" when instantiating "abandonedcart\abandonedcart\controllers\DefaultController".`
Answers.UserDisplayName: offtheshore
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-19T09:19:41.163Z
Answers.Text: Why would you even call a controller action in your `init` function in the first place? That's not intended and bad practice. Controllers are for handling requests and you should route to those actions based on request parameters.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-19T09:26:30.033Z
Answers.Text: It's for testing an action that'll be triggered by an Ajax request. But I'll test with a service in the mean time. Thanks!
Answers.UserDisplayName: offtheshore
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-19T08:56:53.660Z
Answers.LastActivityDate: 2018-04-19T08:56:53.660Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm trying to call an action from my main class in Craft 3. This is working fine with my services like this:</p>
  
  <pre><code>AbandonedCart::$plugin-&gt;serviceExample-&gt;serviceMethod();
  </code></pre>
  
  <p>I've tried to do something similar for my action but it doesn't work and I can't find any solution to it.</p>
  
  <pre><code>AbandonedCart::$plugin-&gt;actionExample-&gt;actionFunction();
  </code></pre>
  
  <p>Any ideas on how to do this?</p>
  
  <p>Thanks in advance,
  Sam</p>
  
Tags: <plugin-development><craft3>
Title: Calling action from main class init()
ThreadText: # Calling action from main class init()
  
  (Asked by: username_0 on 2018-04-19)
  
  I'm trying to call an action from my main class in Craft 3. This is working fine with my services like this:
  
  ```
  AbandonedCart::$plugin->serviceExample->serviceMethod();
  
  ```
  
  I've tried to do something similar for my action but it doesn't work and I can't find any solution to it.
  
  ```
  AbandonedCart::$plugin->actionExample->actionFunction();
  
  ```
  
  Any ideas on how to do this?
  
  Thanks in advance,
  Sam
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-19. Score: 1 | Accepted answer
  
  You need to call your `setComponents` function before you are trying to access your components otherwise - logically - they are not defined
  
  ```
  public function init()
  {
      parent::init();
      self::$plugin = $this;
  
      // define the components / services
      $this->setComponents(
          [
              'serviceExample' => ServiceExample::class
          ];
      );
  
      // access them
      $this->serviceExample->serviceMethod();
      // if it's easier for you to remember you can as well use the longer/slower syntax
      AbandonedCart::$plugin->serviceExample->serviceMethod();
  );
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the tip, this is indeed a better version of what I used. I had to use `services\ServiceExample::class` to get it working with my services. This however doesn't work for actions in my controller. I get the following error: `Missing required parameter "id" when instantiating "abandonedcart\abandonedcart\controllers\DefaultController".`  
  > **username_1**: Why would you even call a controller action in your `init` function in the first place? That's not intended and bad practice. Controllers are for handling requests and you should route to those actions based on request parameters.  
  > **username_0**: It's for testing an action that'll be triggered by an Ajax request. But I'll test with a service in the mean time. Thanks!  
  
  ---
  
  