# Run plugin code on load

**URL:** <https://discourse.cakephp.org/t/run-plugin-code-on-load/5500>\
**Category:** Need Help\
**Created:** [January 8, 2019, 11:01am UTC](https://discourse.cakephp.org/t/run-plugin-code-on-load/5500 "2019-01-08T11:01:27Z")\
**Posts on this page:** 2\
**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:** [January 8, 2019, 11:01am UTC](https://discourse.cakephp.org/t/run-plugin-code-on-load/5500/1 "2019-01-08T11:01:27Z")

</div>

In some past posts of mine, I have tried getting something to work.  
This something is a little piece of code in a plugin that calls on a component in a core plugin.

However, this has gotten me into some issues:

- Placing this code in the `AppController:initialize` results in this code not running at all unless something specific to the plugin is also loaded (eg. a controller in the plugin)
- Placing this code in the `bootstrap.php` results in the app breaking when having an empty database (eg. after having to re-install a workstation)

My question is, where and how should I do this in a proper fashion?  
This is the code I’m trying to run:

```auto
...

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

// Create new settings group
$this->Settings->registerGroup([
  'group'=>'smtp',
  'label'=>'Mailserver Settings'
]);

// Add items to the settings group
$this->Settings->registerOption([
  'type'=>'text',
  'name'=>'smtp_host',
  'value'=>$this->Settings->getOptionValue('smtp_host'), // Here, the issue arrises
  'group'=>'smtp',
  'label'=>'Host',
  'placeholder'=>'Your mailserver\'s address'
]);

...

```

UPDATE 1: While waiting for a proper solution on this issue, I have opted to place the code back in `bootstrap.php` and wrap it in an `if(PHP_SAPI === 'fpm-fcgi'){` to check whether we are using the CLI (so it doesn’t break the cake CLI tool) or a browser.

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [January 10, 2019, 3:31am UTC](https://discourse.cakephp.org/t/run-plugin-code-on-load/5500/2 "2019-01-10T03:31:12Z")

</div>

> [@FinlayDaG33k](#):
>
> Placing this code in the `bootstrap.php` results in the app breaking when having an empty database

This is the correct place, but you need only run the code if the expected table is present. The simplest way is to wrap the troublesome call with a `try {...] catch () {...}`
