Download the PHP package vogon101/dynamic-endpoints without Composer
On this page you can find all versions of the php package vogon101/dynamic-endpoints. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vogon101/dynamic-endpoints
More information about vogon101/dynamic-endpoints
Files in vogon101/dynamic-endpoints
Package dynamic-endpoints
Short Description Super-Simple framework for making nice PHP APIs
License
Homepage https://github.com/BluePost/BlueAjax/
Informations about the package dynamic-endpoints
DynamicEndpoints
A super-simple PHP library for creating nice APIs. It allows you to have urls like api.foo.com/my/endpoint/2
The Idea
When writing an HTTP API in PHP it is much nicer to have a url like this: api.foo.com/books/2
instead of this api.foo.com/books.php?id=2
. This is simple enough to do, use a custom apache 404 page, then parse the URL, but that can be a pain. So I made a tiny library for it.
Usage
For a working example, see the test directory.
.htaccess
In the root folder you have to have a .htaccess
file that defines a main page, perhaps index.php
as the 404 ErrorDoucument. It could look like this:
PHP
First, import the sources:
or use composer and just require your autoload.php
:
Next, create an API object. $base
is the path that you want all the APIs to come off of. For example for something like this: http://foo.com/my-thing/api/
the $base
would be /my-thing/api
. Base can be blank, for something at the root url.
Now, you have to register the endpoints. These define what URLs can be accessed through the API. The keys are the endpoint strings and the values are the files to be run if the endpoint is accessed. They can contain named variables and wildcards:
So /api/book/%id
will match this /api/book/2
and it will pass $id
with value 2. However because /api/book/name/%name
is very similar to the %id
one, if a user were
to navigate to /api/book/name/
then the algorithm will match it to the %id
endpoint, loading it with $id = "name"
Now we simply run the API. This will include the file specified if the enpoint matches. The variables will be set as specified in the scope of the file. If no endpoint is found, and array will be returned with an key of "Error"
.
License
The software is avaliable under the Creative Commons Attribution 4.0 International. It can be found here: http://creativecommons.org/licenses/by/4.0/