-
Notifications
You must be signed in to change notification settings - Fork 0
Get Started
This comprehensive guide explains how to get your first endpoint up and running and do cool stuff. Assuming you have Reflect installed and ready to go, let's dive in!
-
It starts with a folder
Create a folder called
endpoints
in the root of your reflect installation. This is where your code resides.$ mkdir endpoints
-
Public folder
Then create another folder inside
endpoints
calledpublic
. This will contain all entry points for your endpoints.$ mkdir public
You can also
cd
into this directory if you want, as all of the remaining steps of this guide will concern stuff inside this folder. -
Create endpoint
Reflect uses the file structure in the
endpoints
folder to determine what is an endpoint and what is not. So, for example, to expose an endpoint called/ping
you would create a folder calledping
here.$ mkdir endpoints/public/ping
-
Create a PHP file for your endpoint logic
To make your endpoint do more than nothing, create a
.php
file with the same name as your endpoint except with the first letter Capitalized.$ touch endpoints/public/ping/Ping.php
This file is now the "default" endpoint of
/ping
. If you were to create another file called for exampleFoo.php
inside this folder, that would become a second-level endpoint reached via/ping/foo
.Some examples:
endpoints/public/ping/Ping.php ==> https://example.com/ping endpoints/public/ping/Foo.php ==> https://example.com/ping/foo endpoints/public/ping/bar/Baz.php ==> https://example.com/ping/bar/baz endpoints/public/ping/bar/Qux.php ==> https://example.com/ping/bar/qux
-
File contents
So here is where it all begins. Reflect tries to keep the religious framework-specific syntax to a minimum, but say it with me "
stdout
andstderr
". These are the two methods you should remember (inspired by standard streams). You use these two to send a response back to the client, whether they're connected via HTTP or socket.<?php require_once Path::api(); // The name of the class should be the endpoint path as PascalCase. // So for "/ping" the name is "Ping". If the endpoint was "/ping/foo" the class name would be "PingFoo" etc. class Ping extends API { public function __construct() { // Initialize the parent with the HTTP Content-Type you wish the response to be in. // See https://github.com/viwes/reflect/wiki/Supported-technologies#response-content-types for list of available types. parent::__construct(ContentType::JSON); } // Accept REST method by creating a "_<HTTP_METHOD>" public method. // It gets called automagically when the endpoint gets a request. public function _GET() { return $this->stdout("pong"); } }
That's it to get a basic endpoint's logic defined. You can do whatever you want in these
_<HTTP_METHOD>
class methods!
Now that we've come all the way down here, let's dive further into authentication abyss. 🤿
You can use the Reflect admin CLI to manage endpoints, users, keys and permissions. Read more on the Authentication page