[beginner] How to use custom PHP inside endpoind

I’ve just discovered this CMS and it’s perfect for a project. However, it’s not very well documented and I can’t find how to use the custom PHP code for API endpoinds.

For example:

  • How can be validated a call to create a new row inside a location? What’s the structure of the code? How can I get the body?
  • How can I override the return of a get call on a collection?

Most of the platform that I need to make can be configured with no-code, but some parts need some programming. I don’t know how to use these:

Many thanks and great job with this headless CMS. I’ll try to contribute by developing some more documenation.

1 Like

Well, fast forward two weeks and let me tell you what I’ve discovered.

That screenshot I’ve uploaded it allows php code, but not for I wanted. Using a combination of debugging tools and some posts, this code is to write custom code for access purposes. For example, imagine that you only want to disallow edit for certain user, you could do this by specifying the user and returning false.

Fow what I wanted to do, I needed to create a custom endpoint.

It’s a shame that this project is so under documented, because it’s great. It shove off at least two weeks of work, but if well documented, it could have saved me even more. When I have more time I’ll try to contribute by documenting at least what I discovered.

1 Like

Hi

Have you got an example of how you used this please?

Yes, you need to create a PHP file inside config/api/public.

For example, let’s image that I create a file called auth.php inside that folder. Then you can make a request to http://[whateverdomain]/public/auth.

I can’t share details of the file, but it your request is a POST you will have all the data you need in $_POST (native global PHP variable). From here you can program whatever you need using a combination of native PHP actions and to access data i’d suggest using the ORM of cockpit.

Example to get a record from a collection:

    $options = [
        "filter" => [
            '$and' => [
                ["email" => "test@example.com"]]
        ],
        "limit" => 1
    ];
    $entries = $self->module('collections')->find("users", $options);

With this it’ll be much easier for you to find more information. Hope it helps!