Download the PHP package idammi/coolapi without Composer
On this page you can find all versions of the php package idammi/coolapi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package coolapi
Cool API (by Robert Grubb)
A new PHP API framework that is simple and fast. I forked this repo for my private project.
Installation
Make sure you have rewrite mod enabled, and you place the following in .httaccess
where your public folder is located. NOTE: CoolApi will throw an exception if it's not found.
NOTE: The API will attempt to do this soon for you if the directory is writable
composer require idammi/coolapi
Configuration Explained
All values below are the defaults set by the API.
Example of Usage
Routing
To add a route for CoolApi, it's as simple as the following:
And is the same for POST, PUT, or DELETE
Using parameters in the route itself:
If the parameter does not exist, it will return false.
To get all parameters from the request: $req->params
.
$api->router->use()
The use()
method allows you to use an array of routes that can be imported from other files to organize your code in a simple way. Below is an example of how to define a route in another file, and export it.
userRoutes.php
index.php
Below is how you would import userRoutes.php
Now you can access /user/:id/data
as a GET route.
Use of $req
Getting POST, or GET variables:
Getting parameters from the URL:
Getting headers from the request
Getting a specific header:
Use of $res
Returning a normal response:
Setting the status:
Setting the content type:
Enabling Cors Layer
Above will allow all sites to access your api.
Above only allows requests from google.com
Above allows requests from anywhere except google.com
Requiring an API Key in the request
You can use the following configuration to require an API key during the request to lockdown your API.
keyField
is looked at only if the request does not include a Authorization: Bearar ?key=
in the url, or $_POST['key']
.
Setting an origin for a specific key:
Now the key thisisanapikey
is only accessible from the origin www.facebook.com
Using Middleware
This api is setup so you can use your own middleware in the routes. Below is an example: