Answers.Body: <p>I found the issue. The author of the tutorial I copied my first code from made the unfortunate choice to not use a proper plugin class:</p>
  
  <pre><code>class Plugin extends \craft\base\Plugin { ... }
  </code></pre>
  
  <p>To fix the problem, I replaced this line with</p>
  
  <pre><code>use Craft;
  use craft\base\Plugin;
  class Yutulic extends Plugin { ... }
  </code></pre>
  
  <p>and had to add a reference to the class to my controller and service modules:</p>
  
  <pre><code>use ns\yutulic\Yutulic;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-20T05:30:54.667Z
Answers.LastActivityDate: 2020-02-20T05:30:54.667Z
Answers.UserDisplayName: Crafty Cat
Body: <p>I'm sure this is a trivial mistake but I can't figure it out (first-time plugin dev).</p>
  
  <p>I have my plugin running with all code in the controller and want now follow best practice and move the workhorse code into a service class.</p>
  
  <p>My main plugin code is</p>
  
  <pre><code>namespace ns\yutulic;
  use ns\yutulic\models\SettingsModel;
  use craft\models\Section;
  
  public function init() {
  
      parent::init();
  
      self::$plugin = $this;
  
      $settings = $this-&gt;getSettings();
      $settings-&gt;sectionOptions = $this-&gt;setSectionOptions();
  
      // Register services
      $this-&gt;setComponents([
          'videoentries' =&gt; \ns\yutulic\services\VideoEntriesService::class,
      ]);
  
      $data = $this-&gt;videoentries-&gt;scanForVideos(null); // works
  
  }
  </code></pre>
  
  <p>The relevant controller code is</p>
  
  <pre><code>Class ScanController extends Controller {
      public function actionScanEntries() {
          // Neiter works :-(
          //$data = Yutulic::$plugin-&gt;videoentries-&gt;scanForVideos($id);
          $data = Yutulic::getInstance()-&gt;videoentries-&gt;scanForVideos($id); // (*)
      }
  }
  </code></pre>
  
  <p>And my service code:</p>
  
  <pre><code>&lt;?php
  namespace ns\yutulic\services;
  use ns\yutulic\Yutulic;
  use Craft;
  use craft\base\Component;
  
  class VideoEntriesService extends Component
  {
      public function scanForVideos($sectionId = null) {
          echo "Works!!!";
          return null;
      }
  }
  </code></pre>
  
  <p>Even if I use the <a href="https://docs.craftcms.com/v3/extend/services.html#creating-a-service" rel="nofollow noreferrer">example of the docs</a> it fails with the same error message on the line indicated (*) in the controller (but with the 'Foo' class name):</p>
  
  <pre><code>Class 'ns\yutulic\Yutulic' not found
  </code></pre>
  
  <p>This shouldn't be so hard?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-19T09:08:55.477Z
Comments.Text: What's the name and class of your main plugin file?
Comments.UserDisplayName: RoussKS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-20T22:05:50.990Z
Comments.Text: @RoussKS That was indeed the problem (see my answer below). I had copied code from an example that was different from all other plugins I reviewed. Eventually it dawned on me that the class name was wrong.
Comments.UserDisplayName: Crafty Cat
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-21T07:39:30.190Z
Comments.Text: It was an instant thought (even though simplistic. Happens to the best of us :) glad you sorted it out.
Comments.UserDisplayName: RoussKS
Tags: <plugin-development><craft3>
Title: Cannot call service: "'MyPlugin' not found" error
ThreadText: # Cannot call service: "'MyPlugin' not found" error
  
  (Asked by: username_0 on 2020-02-19)
  
  I'm sure this is a trivial mistake but I can't figure it out (first-time plugin dev).
  
  I have my plugin running with all code in the controller and want now follow best practice and move the workhorse code into a service class.
  
  My main plugin code is
  
  ```
  namespace ns\yutulic;
  use ns\yutulic\models\SettingsModel;
  use craft\models\Section;
  
  public function init() {
  
      parent::init();
  
      self::$plugin = $this;
  
      $settings = $this->getSettings();
      $settings->sectionOptions = $this->setSectionOptions();
  
      // Register services
      $this->setComponents([
          'videoentries' => \ns\yutulic\services\VideoEntriesService::class,
      ]);
  
      $data = $this->videoentries->scanForVideos(null); // works
  
  }
  
  ```
  
  The relevant controller code is
  
  ```
  Class ScanController extends Controller {
      public function actionScanEntries() {
          // Neiter works :-(
          //$data = Yutulic::$plugin->videoentries->scanForVideos($id);
          $data = Yutulic::getInstance()->videoentries->scanForVideos($id); // (*)
      }
  }
  
  ```
  
  And my service code:
  
  ```
  <?php
  namespace ns\yutulic\services;
  use ns\yutulic\Yutulic;
  use Craft;
  use craft\base\Component;
  
  class VideoEntriesService extends Component
  {
      public function scanForVideos($sectionId = null) {
          echo "Works!!!";
          return null;
      }
  }
  
  ```
  
  Even if I use the [example of the docs](https://docs.craftcms.com/v3/extend/services.html#creating-a-service) it fails with the same error message on the line indicated (\*) in the controller (but with the 'Foo' class name):
  
  ```
  Class 'ns\yutulic\Yutulic' not found
  
  ```
  
  This shouldn't be so hard?
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_1**: What's the name and class of your main plugin file?  
  > **username_0**: @username_1 That was indeed the problem (see my answer below). I had copied code from an example that was different from all other plugins I reviewed. Eventually it dawned on me that the class name was wrong.  
  > **username_1**: It was an instant thought (even though simplistic. Happens to the best of us :) glad you sorted it out.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-02-20. Score: 1
  
  I found the issue. The author of the tutorial I copied my first code from made the unfortunate choice to not use a proper plugin class:
  
  ```
  class Plugin extends \craft\base\Plugin { ... }
  
  ```
  
  To fix the problem, I replaced this line with
  
  ```
  use Craft;
  use craft\base\Plugin;
  class Yutulic extends Plugin { ... }
  
  ```
  
  and had to add a reference to the class to my controller and service modules:
  
  ```
  use ns\yutulic\Yutulic;
  
  ```
  
  
  ---
  
  