API - Username instead of id on _by/_mby

I don’t think the official docs have the detail about building addons and doing some logic on hook events.
I can share you how I understand it.

Disclaimer - the below information is based on my experience, so I could be wrong

config/bootstrap.php is like an init file. But it’s used for general purpose and you can add your common codes inside it.

For a specific type of init file, you can create your own addon under addons directory.
So, it would be something like this - addons/{your_addon_name}/bootstrap.php


Let me explain how a very simple addon works.

if (COCKPIT_API_REQUEST) {
    $app->on('cockpit.rest.init', function ($routes) use ($app) {
        $routes['test'] = function () use ($app) {
             $my_variable = $this->param('my_variable'); 
        }
     } 
}

API Authentication

COCKPIT_API_REQUEST is API authentication / middleware

if(COCKPIT_API_REQUEST){
    // The requests must includes token to access this 
}

Event

cockpit.reset.init is like your codes will execute when Cockpit CMS init.
This is very similar to Jquery or JS events.

 $app->on('cockpit.rest.init', function ($routes) use ($app) {
         // do your logic here
     } 

So, you might say how do I know those events?
Here is the list of Cockpit events.


Routes

To declare routes, you can write as below.

if (COCKPIT_API_REQUEST) {
    $app->on('cockpit.rest.init', function ($routes) use ($app) {
        $routes['test'] = function () use ($app) {
             // access api route at "api/test" and do your logic here 
       }
    })
}

This is an example test route, so you can access your route like /api/test


Request Parameters

You can simply grab the request parameter like $this->param("my_variable")

e.g

     $my_variable = $this->param('my_variable'); 

Lime framework

Cockpit was built on top of Lime micro php framework https://github.com/agentejo/lime
So, most of it basic functions would be the same as lime.

You can also learn from this scripts list by @raffaelj

Hope this also help someone.

3 Likes