Query Data and Concept


#1

Hi,

I think I misunderstand this CMS. Do I need to query the data out of a colletion via an API on the client or is there for E.G. a PHP Library. Or do I just need to query the SQLite or MongoDB or whatever it uses?

I think I don’t get the concept since if you use the Example JS on the client every1 can edit the collections


#2

indeed you seem to have missunderstood the concept.

Cockpit is based on api requests/calls so of course querying the database behind cockpit your self would not make any sense at all because thats what the api is designed and there for.

Preventing people from writing data to your collections is done via ACL (access control lists) / gorup management. Your webpage/app uses the api key of an unprivileged user who has only read access. You your self instead will use an admin oder management user who can do everything (and so can update collections and their data). On this way you are able to protect your data.

Querying the cockpit api can be done on multiple ways. You can do it with JS / async requests whats probably the best way, or you can do it using PHP.

There is no official PHP API wrapper but I began to write one my self for my own projects (this was actually not designed to be released to the public). There is still much to do and much untested stuff within but its constantly growing as I need stuff.

I published my current working state for your. Perhaps you can use it in any way:

usage:

// your page_or_site.php:
CockpitHelper::$_COCKPIT_RESTAPI_TOKEN = null; // set the api key of the user that has read access to the collections your page needs to acces... null = requests will use public access. If there are no collection that have public access configured you wont get any result without setting an api key
CockpitHelper::$_COCKPIT_BACKEND_URL = 'https://admin.yourdomainthathascockpitinstalledbehind.com';
$news = CockpitHelper::getCollectionEntries('News', ['published' => 1, 'foo' => 'bar'])

#3

Thanks for sharing this information and code, The reason I want to avoid JS for this right now is because I need to prerender the articles to my page else google will not index it.

So I wanted to pass it via PHP, I will look at your helper class and extend and update it where I need and share it back to you when I’m done. So we help each other.

About that ACL I need to have a look but thanks bout that.

EDIT:
Using the slim framework I wrote a small proxy if I can call it like that. to avoid showing the backend url what is not a big deal but just because it also looks nicer.

any('/api[/{params:.*}]', function (ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, array $args) {
    // Guzzle HTTP
    $client = new Client;
    $HTTP_HOST = $request->getHeader('HTTP_HOST')[0];
    // `/backend` is where cockpit is located
    $URL = 'http://' . $HTTP_HOST . '/backend'; // TODO: Get Request Scheme
    $res = $client->request($request->getMethod(), $URL . $request->getServerParams()['REQUEST_URI']);
    // ValidateJSON is a custom function to validate my json
    if (json_validate($res->getBody())) {
        $data = json_decode($res->getBody(), TRUE);
        return $response->withJson($data['entries'], 201);
    } else {
        return $response->withRedirect('/', 302);
    }
});

#4

@xJoeyv, maybe you need to think in a different approach, like using GatsbyJS or React Static for your frontend website, things like SEO are handled properly


#5

While I prefer PHP for the things Im also gonna add to my website. Next to that I found my approach and I query them within the server like @serjoscha87.

Didn’t think of that way myself… :slight_smile:

But @serjoscha87 I’m working on my own CockpitClient that I will release but I took this approach.

$workEntries = $this->db->entries('work');
$workFields = $this->db->fields('work');
$workFieldss = $this->db->fields('work', true); // Not made yet but will process the fields

$this->db is because I use Slim Framework and I resolve the db service

My Function but I share the rest later. Im fixing the major calls and Ill release the first version on Packagist.

/**
     * @param $collection
     * @return false|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Exception
     */
    public function fields($collection, $process = false)
    {
        if($process === true) {
            throw new \Exception('Not implemented');
        }
        $this->_request = new CockpitRequest('GET', Endpoint::getCollection);
        $this->_request->setQuery('fields');
        $this->_request->addParam('name', $collection);
        return $this->getJson();
    }