# Load component on plugin run?

**URL:** <https://discourse.cakephp.org/t/load-component-on-plugin-run/5407>\
**Category:** Uncategorized\
**Created:** [December 15, 2018, 4:15pm UTC](https://discourse.cakephp.org/t/load-component-on-plugin-run/5407 "2018-12-15T16:15:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 15, 2018, 4:15pm UTC](https://discourse.cakephp.org/t/load-component-on-plugin-run/5407/1 "2018-12-15T16:15:50Z")

</div>

Hii there,  
In our project, we have a component that allows us to easily add options to our database form our plugins (a bit like WordPress does it).  
For this, we need to run a small piece of code in the plugin to tell the component a new setting is there:

```auto
$this->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
 $this->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$this->Settings->registerOption('text','smtp[host]','','contact');

```

I’ve added this to the `AppController` of the plugin, but this gives a small issue since this code is never ran until we actually do something with the plugin itself.  
This is a bit of an issue since it doesn’t use the plugin itself, the plugin basically just tells to the main app: “hey, I’m here, and these are the settings I want you to show the user (so he/she can change it without having to code)”.

Where should I add this code in the plugin (not in the main app) so that it will run when the plugin it loaded?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 15, 2018, 4:54pm UTC](https://discourse.cakephp.org/t/load-component-on-plugin-run/5407/2 "2018-12-15T16:54:03Z")

</div>

Seems like this is maybe the sort of thing that the `bootstrap` option of the plugin loader is for?

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 15, 2018, 7:11pm UTC](https://discourse.cakephp.org/t/load-component-on-plugin-run/5407/3 "2018-12-15T19:11:12Z")

</div>

I imagined that too yes, but this only seems to break the app.  
I placed the code in plugin’s `bootstrap.php` like so:

```auto
<?php 
use Cake/Core/Plugin;

$this->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
$this->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$this->Settings->registerOption('text','smtp[host]','','contact');

```

but this results in: the error: `Call to undefined method Kikioboeru\Contact\Plugin::loadComponent()`.

EDIT: I’ve hacked around it a little bit and put this code in the `bootstrap.php` of the plugin:

```auto
use Cake\Controller\Controller;

$controller = new Controller();
$controller->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
$controller->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$controller->Settings->registerOption('text','smtp[host]','','contact');

```

While this does _sorta_ work (it adds it to the `options` property just fine), when I go to the `SettingsController` and add the following code, the `options` property is empty again (probably because they are two seperate instances):

```auto
pr($this->Settings->options);

```

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 16, 2018, 11:41am UTC](https://discourse.cakephp.org/t/load-component-on-plugin-run/5407/4 "2018-12-16T11:41:20Z")

</div>

I solved this issue by making use of (read: abuse) the `Configure` class instead of making it a property in the component class.
