# How to create a addon

**URL:** <https://discourse.getcockpit.com/t/how-to-create-a-addon/345>\
**Category:** Addons\
**Created:** [October 19, 2018, 10:42pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345 "2018-10-19T22:42:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xJoeyv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/xjoeyv/32/116_2.png) [@xJoeyv](https://discourse.getcockpit.com/u/xJoeyv)\
**Post date:** [October 19, 2018, 10:42pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/1 "2018-10-19T22:42:38Z")

</div>

Hi,

I wonder if there are any docs on how to create a new addon?  
And after that if there is any API documentation for it aswell.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![serjoscha87](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/serjoscha87/32/151_2.png) [@serjoscha87](https://discourse.getcockpit.com/u/serjoscha87)\
**Post date:** [October 19, 2018, 10:51pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/2 "2018-10-19T22:51:31Z")

</div>

No there is no documentation on how to create addons.

I recommend looking at other addons in order to understand how to create one your self.

---

<div class="post-metadata">

**Author:** ![xJoeyv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/xjoeyv/32/116_2.png) [@xJoeyv](https://discourse.getcockpit.com/u/xJoeyv)\
**Post date:** [October 22, 2018, 5:19pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/3 "2018-10-22T17:19:53Z")

</div>

Alright. Kinda bad since this does not invite to get more addons and extemd the community. But ill check it out

---

<div class="post-metadata">

**Author:** ![serjoscha87](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/serjoscha87/32/151_2.png) [@serjoscha87](https://discourse.getcockpit.com/u/serjoscha87)\
**Post date:** [October 23, 2018, 6:55am UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/4 "2018-10-23T06:55:55Z")

</div>

Cockpit is stil a free one person project. Its quite simple: If you want support you can still pay for it. If not you have to take what you can get from the community and self found sources.

---

<div class="post-metadata">

**Author:** ![pauloamgomes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pauloamgomes/32/48_2.png) [@pauloamgomes](https://discourse.getcockpit.com/u/pauloamgomes)\
**Post date:** [October 23, 2018, 8:15am UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/5 "2018-10-23T08:15:37Z")

</div>

@xJoeyv it will require some time to have more users involved and participating, that’s the way opensource projects usually work. The addon architecture is quite simple, as @serjoscha87 said you can learn by inspecting the existing ones.

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 23, 2018, 11:26pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/6 "2018-10-23T23:26:05Z")

</div>

Cockpit has a lot of ways to get modified. A few of them are documented. If you read anything in the issues or in this forum about adding code to `config/bootstrap.php`, this same code could be in an addon with the same effect. The difference here is, if you want to modify it fast and simple or if you want to provide a reusable addon.

Addons are handled the same like core modules (collections, singletons, forms). So, each addon will be auto-registered as a cockpit module. Theoretically, there is no difference, if you put your addon in `/modules` or in `/addons`. There is some logic to auto-register the path name to short forms when linking assets etc. But this is advanced stuff for now.

**First steps to create an addon**

- create a folder `customaddon` in `path/to/cockpit/addons`
- add a file `bootstrap.php` in the root of `/customaddon`
- write your custom code
  - choose an event
  - modify anything

- if you add assets or custom controllers (classes), try to follow the naming convention of the core modules to keep it clearly

**content of `customaddon/bootstrap.php`**

Now it’s complicated. There are so much possible ways… It depends on what you want to achieve. The most common use case is to add some checks or to modify data when an event is fired. Another use case is to modify the UI.

Let’s start with a very simple example and add ‘Hello world!’ to the top of the UI.

```php
$app->on('app.layout.contentbefore', function(){

    echo 'Hello world!';
    // Obviously, this was useless. Let's discover the whole structure of cockpit instead:
    echo '<pre>' . print_r($this, true) . '</pre>';

});

```

The next example adds absolute paths to assets meta data, when an asset is saved.

```php
$app->on('cockpit.assets.save', function(&$assets) use ($app) {
    
    foreach ($assets as &$asset) {
        
        // add paths
        $asset['absolute_path'] = $app['site_url'] . $app['base_url'] . '/storage/uploads' . $asset['path'];
        
        $asset['real_path'] = COCKPIT_DIR . '/storage/uploads' . $asset['path'];
        
    }
    
});

```

Now let’s replace the dashboard with the entries view of a collection named “pages”:

```php
$app->on('admin.init', function() {

    $this->bind('/', function(){
        return $this->invoke('Collections\\Controller\\Admin', 'entries', ['collection' => 'pages']);
    });

    $this->bind('/cockpit/dashboard', function(){
        $this->reroute('/');
    });

});

```

Now it should be clear, why there is no explicit way to create an addon. You can extend cockpit with anything, you can imagine and there are several ways to reach your goal.

Enough examples… if you want some inspiration, read the code from other addons, search the issues, read the core code and ask for help. Also keep in mind, that sometimes your problem is interesting enough to get answered immediately and sometimes…

> [@pauloamgomes](#):
>
> it will require some time to have more users involved and participating, that’s the way opensource projects usually work.

I also collected a few code snippets for multiple use cases. The readme contains a list of addons, I wrote, too:

> **[GitHub - raffaelj/cockpit-scripts: A collection of code snippets with...](https://github.com/raffaelj/cockpit-scripts)**
>
> A collection of code snippets with annotations to modify Cockpit CMS - GitHub - raffaelj/cockpit-scripts: A collection of code snippets with annotations to modify Cockpit CMS

A list with all events (scroll down or use the search function to find the files where they are fired and the possible/necessary arguments):

> <https://github.com/raffaelj/cockpit-scripts/blob/master/cli/get-triggers_list-of-events.md>

---

<div class="post-metadata">

**Author:** ![xJoeyv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/xjoeyv/32/116_2.png) [@xJoeyv](https://discourse.getcockpit.com/u/xJoeyv)\
**Post date:** [October 24, 2018, 8:28am UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/7 "2018-10-24T08:28:56Z")

</div>

Got dayum this is such a good way to start. Can you not create a topic and lock it and pin it to the top?

Also I will look into the stuf, I know want to make a couple addons for new fieldtypes or add options to existing ones. But you explaination is awesome. Thanks

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 26, 2018, 1:34pm UTC](https://discourse.getcockpit.com/t/how-to-create-a-addon/345/8 "2018-10-26T13:34:21Z")

</div>

Thanks for the compliment 🙂 Actually, I started the post with “Let me try a very short summary” and when I finished writing, I had to delete the first sentence.

> [@xJoeyv](#):
>
> Can you not create a topic and lock it and pin it to the top?

I think about it, but your topic exists already and it has the right title if others search for it. If I produce nice text blocks for the “advanced stuff” in the future, they all could merged to a development guide.

Which new field types do you have in mind?
