Cheatsheet connect getcockpit with vanilla PHP

Put Cockpit folder in same directory

  • project Folder
    – cockpit
    – post-detail.php
    – posts.php
    – contact.php

posts.php

<?php
require_once('./cockpit/bootstrap.php');
$limit = 5;

    $posts = cockpit('collections')->find('posts', [
        'filter' => ['published' => true],
        'limit' => $limit,
        'sort' => ['_o' => -1],
    ]);

foreach ($posts as $post) { ?>
<h1><?=$post['title']?></h1>
<p><?=$post['content']?></p>
<?php } ?>


contact.php

add data to form

<?php
require_once('./cockpit/bootstrap.php');

if($isset($_POST["btnSubmit"])){
        $params = [];
        $data = array(
            "fullname" => $_POST['fullname'],
            "companyname" => $_POST['companyname'],
            "email" => $_POST['email'],
            "phone" => $_POST['phone'],
            "message" => $_POST['message']
        );
        cockpit('forms')->submit('contactform', $data, $params);
}else{

}
?>
<form>
...
</form>


post_detail.php

<?php
require_once('./cockpit/bootstrap.php');

$post = cockpit('collections')->find('posts', [
    'filter' => ['title_slug' => 'blog-1', 'published' => true]
]);
$post = $post[0];
?>
<h1><?=$post['title']?></h1>
<p><?=$post['content']?></p>

Singleton

$settings = cockpit('singletons')->getData('general_settings');
//you can see the settings array

print_r($settings)

Navigation.php

//return tree array dont forget active custom sortable entries in collection

![Screen Shot 2021-10-26 at 12.15.15|617x500](upload://eSkGTutZ3RmzYkOrEzoMcIo6iKZ.png)

$navs = cockpit('collections')->find('menu', [
	'limit' => 100,
    'buildtree' => true
]);

For User in cokcpit only via API

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://domain.com/api/cockpit/listUsers?token=XXXX");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$users = curl_exec($curl);
curl_close($curl);

      $json = json_decode($users);
      foreach ($json as $user) {
echo $user->email;
echo $user->id
      
      }

You can see the All function:
Collections
cockpit/modules/Collections/bootstrap

Form
cockpit/modules/Forms/bootstrap

Singleton
cockpit/modules/Singletons/bootstrap

7 Likes

You can also use Guzzle (to fetch data via Cockpit API) and Slim (micro-framework, to manage the routing part) to do this :

:wink:

2 Likes

Hi creativeplusplus, how did you manage the routing of the pages and the rewriting of the url of the jobs?
Thanks if you want to answer me.

@creativeplusplus Only recently found Cockpit and in the middle of my first site - this post has been incredibly helpful!!

Many thanks!