Answers.Body: <p>You can do this in Composer with the following:</p>
  
  <pre><code>"extra": {
      "name": "My Plugin",
      "handle": "my-plugin",
      "hasCpSettings": false,
      "hasCpSection": false,
      "components": {
          "myPluginService": "author\\myplugin\\services\\MyPluginService"
      },
      "class": "prove\\myplugin\\MyPlugin"
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-05T09:36:48.223Z
Answers.Text: Thanks. Just adding that "composer dump-autoload" doesn't do the trick here and that you need to remove and then require the plugin again.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-16T15:42:34.943Z
Answers.LastActivityDate: 2019-04-16T15:42:34.943Z
Answers.UserDisplayName: fxfuture
Answers.Body: <p>If you have a module instead of a plugin, make sure your new service is registered in the <code>config/app.php</code></p>
  
  <p>Like so:</p>
  
  <pre><code>return [
      'modules' =&gt; [
          'core-module' =&gt; [
              'class' =&gt; \modules\coremodule\CoreModule::class,
              'components' =&gt; [
                  'member' =&gt; [
                      'class' =&gt; 'modules\coremodule\services\Member',
                  ],
                  'invoices' =&gt; [
                      'class' =&gt; 'modules\coremodule\services\Invoices',
                  ],
              ],
          ],
      ],
      'bootstrap' =&gt; ['core-module'],
  ];
  </code></pre>
  
  <p>Also, make sure that you setting your services in the main plugin/module .php file.</p>
  
  <pre><code>use modules\coremodule\services\Invoices as InvoicesService;
  // .....
  $this-&gt;setComponents([
       'invoices' =&gt; InvoicesService::class,
  ]);
  </code></pre>
  
  <p>In this case, my new service is called Invoices.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-05T14:47:21.353Z
Answers.LastActivityDate: 2019-12-05T14:57:04.933Z
Answers.LastEditDate: 2019-12-05T14:57:04.933Z
Answers.UserDisplayName: José Veríssimo
Answers.Body: <p>It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components":</p>
  
  <pre><code>namespace example\myplugin;
  
  use example\myplugin\services\Example as ExampleService;
  
  use Craft;
  use craft\base\Plugin as BasePlugin;
  
  class MyPlugin extends BasePlugin
  {
      public static $plugin;
  
      public function init()
      {
          parent::init();
          self::$plugin = $this;
  
          $this-&gt;setComponents([
              'example' =&gt; ExampleService::class
          ]);
      }
  }
  </code></pre>
  
  <p>Then, somewhere else in your plugin, your service is immediately available as:</p>
  
  <pre><code>MyPlugin::$plugin-&gt;example-&gt;myMethod();
  </code></pre>
  
  <p>…where <code>example</code> is the key in the hash of classes you passed to <code>$this-&gt;setComponents()</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-16T09:52:37.450Z
Answers.Text: This works but I'd love to know how you achieve the same result with Composer. It doesn't feel right having some services registered with composer and others in the plugin file itself.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-16T15:43:04.203Z
Answers.Text: @ClivePortman see my answer
Answers.UserDisplayName: fxfuture
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-25T21:51:38.787Z
Answers.LastActivityDate: 2018-05-25T21:51:38.787Z
Answers.UserDisplayName: August Miller
Body: <p>In Craft 2, services were auto-loaded when using the <code>craft()-&gt;plugin_serviceName-&gt;myMethodName()</code> signature, but it seems like there's a catch in Craft 3, when using the new <code>MyPlugin::getInstance()-&gt;myServiceName-&gt;myMethod()</code>.</p>
  
  <p>I'm working on a business-logic plugin using a local <code>path</code> repository in my project's <code>composer.json</code>, and there are a rash of problems that come of this, mainly to do with creating + renaming services, as the service locator seems to cache them pretty aggressively.</p>
  
  <p>How can I ensure these changes are picked up right away?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-25T21:52:48.380Z
Comments.Text: Reason for asking: I was having to edit `composer.json` and re-install my plugin (and sometimes edit Craft's `plugins.php` cache) every time I changed a service.
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-16T18:04:03.597Z
Comments.Text: Yeah, its ridiculous having to use a composer.json file.
Comments.UserDisplayName: Dan Zuzevich
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-25T16:04:19.593Z
Comments.Text: Also worth noting, since this seems like it might be an early point of frustration for folks getting started with extending Craft and/or plugin development: be sure and evaluate using a _Module_ over of a _Plugin_, in your project!
Comments.UserDisplayName: August Miller
Tags: <plugin-development><craft3><php>
Title: How do I "register" a service in a Craft 3 plugin?
ThreadText: # How do I "register" a service in a Craft 3 plugin?
  
  (Asked by: username_0 on 2018-05-25)
  
  In Craft 2, services were auto-loaded when using the `craft()->plugin_serviceName->myMethodName()` signature, but it seems like there's a catch in Craft 3, when using the new `MyPlugin::getInstance()->myServiceName->myMethod()`.
  
  I'm working on a business-logic plugin using a local `path` repository in my project's `composer.json`, and there are a rash of problems that come of this, mainly to do with creating + renaming services, as the service locator seems to cache them pretty aggressively.
  
  How can I ensure these changes are picked up right away?
  
  **Tags:** plugin-development, craft3, php
  
  > **Comments:**
  > **username_0**: Reason for asking: I was having to edit `composer.json` and re-install my plugin (and sometimes edit Craft's `plugins.php` cache) every time I changed a service.  
  > **username_1**: Yeah, its ridiculous having to use a composer.json file.  
  > **username_0**: Also worth noting, since this seems like it might be an early point of frustration for folks getting started with extending Craft and/or plugin development: be sure and evaluate using a _Module_ over of a _Plugin_, in your project!  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2018-05-25. Score: 5 | Accepted answer
  
  It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components":
  
  ```
  namespace example\myplugin;
  
  use example\myplugin\services\Example as ExampleService;
  
  use Craft;
  use craft\base\Plugin as BasePlugin;
  
  class MyPlugin extends BasePlugin
  {
      public static $plugin;
  
      public function init()
      {
          parent::init();
          self::$plugin = $this;
  
          $this->setComponents([
              'example' => ExampleService::class
          ]);
      }
  }
  
  ```
  
  Then, somewhere else in your plugin, your service is immediately available as:
  
  ```
  MyPlugin::$plugin->example->myMethod();
  
  ```
  
  …where `example` is the key in the hash of classes you passed to `$this->setComponents()`.
  
  > **Comments:**
  > **username_3**: This works but I'd love to know how you achieve the same result with Composer. It doesn't feel right having some services registered with composer and others in the plugin file itself.  
  > **username_2**: @username_3 see my answer  
  
  ---
  
  ### Answer by username_2 on 2019-04-16. Score: 5
  
  You can do this in Composer with the following:
  
  ```
  "extra": {
      "name": "My Plugin",
      "handle": "my-plugin",
      "hasCpSettings": false,
      "hasCpSection": false,
      "components": {
          "myPluginService": "author\\myplugin\\services\\MyPluginService"
      },
      "class": "prove\\myplugin\\MyPlugin"
  }
  
  ```
  
  > **Comments:**
  > **username_3**: Thanks. Just adding that "composer dump-autoload" doesn't do the trick here and that you need to remove and then require the plugin again.  
  
  ---
  
  ### Answer by username_4 on 2019-12-05. Score: 2
  
  If you have a module instead of a plugin, make sure your new service is registered in the `config/app.php`
  
  Like so:
  
  ```
  return [
      'modules' => [
          'core-module' => [
              'class' => \modules\coremodule\CoreModule::class,
              'components' => [
                  'member' => [
                      'class' => 'modules\coremodule\services\Member',
                  ],
                  'invoices' => [
                      'class' => 'modules\coremodule\services\Invoices',
                  ],
              ],
          ],
      ],
      'bootstrap' => ['core-module'],
  ];
  
  ```
  
  Also, make sure that you setting your services in the main plugin/module .php file.
  
  ```
  use modules\coremodule\services\Invoices as InvoicesService;
  // .....
  $this->setComponents([
       'invoices' => InvoicesService::class,
  ]);
  
  ```
  
  In this case, my new service is called Invoices.
  
  
  ---
  
  