Custom Endpoint - Fetch collection in database

Hello everybody !
I’m new to cockpit (and PHP) and i need your help.
I’m using v2.8.3.
I need a endpoint similar to localhost:80/api/content/item/ITEMNAME but who will return all items, all my singletons.

I created a /config/api/singletons/find.php file and i’m trying to make this work.

See below my files, with all my unsuccessfull attempts :

<?php

// custom api endpoint in `/config/api/singletons/find.php`

require 'cockpit/bootstrap.php';

$singletons = Cockpit::instance()->module('content')->find($this->param('collection')); // 404 when calling the route
// $singletons = $this->storage->find('singletons', $options); // created the route but crashes on storage
// $singletons = $this->module('collections')->find('content-singletons'); // creates the route but crashes "find on null"
// $singletons = $this->module('singletons') // 404 when calling the route
// $count = cockpit('collections')->count($this->param('collection')); // creates the route but crashes "cockpit unknown function"
return $singletons;

Thanks !

Can you be more detailed about what you want to achieve?

Hi :slight_smile:
Of course, what i want is to retrieve all singletons and their data, in one query.
If i have 2 singletons :

content/item/SINGLETON1
{
"label1":"mylabel1",
"boolean1":true
}
content/item/SINGLETON2
{
"label2":"mylabel2",
"boolean2":false
}

I want to be able, using a custom route, to get :

[
{
"label1":"mylabel1",
"boolean1":true
},
{
"label2":"mylabel2",
"boolean2":false
}
]

Or all in one big object, fields won’t have same name anyway.
Thanks

Not tested:

<?php

// custom api endpoint in `/config/api/all-singletons.php`

$singletons = array_filter($this->module('content')->models(), fn($m) => $m['type'] == 'singleton');

$data = [];

foreach ($singletons as $name => &$meta) {
    $data[$name] = $this->module('content')->item($name);
}

return $data;


1 Like

Wow, it works, thanks a lot Artur !!