Download the PHP package williamheelis/restful-inputs without Composer
On this page you can find all versions of the php package williamheelis/restful-inputs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download williamheelis/restful-inputs
More information about williamheelis/restful-inputs
Files in williamheelis/restful-inputs
Package restful-inputs
Short Description Simple RESTful globals: $_PUT, $_PATCH, $_DELETE, $_HEADER, $_JSON, $_PATH, $_RES for PHP APIs. Basically this is an extension of the logic PHP probably intended when they introduced _GET and _POST and this package simply extends it and is useful shorthand option for RESTful api end points as a result. The focus was on inline brevity, and assumes plenty of `/api/someItem/index.php` -> `PATCH::/api/someItem/` structure. (i.e lots of dirs with one index.php each!)
License MIT
Informations about the package restful-inputs
Restful Inputs for PHP
Automatically sets useful REST globals in plain PHP:
$_HEADER: request headers$_PUT,$_PATCH,$_DELETE: request body data (ifInputs::is('PUT')it means to was sent PUT)$_JSON: parsed JSON body (ifInputs::is('JSON')it means to was sent JSON)$_PATH: parsed path parameters (EG: a input mask ofInputs::setPath('api/someItem/{uid}')will make$_PATH['uid']available)$_RES: response wrapper sent automatically on shutdown - use ['data'], ['status_code'] and ['headers']
TLDR; Auto Behaviour Summary
| Request | When Available |
|---|---|
$_HEADER |
Always — all request headers |
$_JSON |
Only if body is JSON |
$_PUT |
Only on PUT request |
$_PATCH |
Only on PATCH request |
$_DELETE |
Only on DELETE request |
$_PATH |
After Inputs::setPath() or fallback |
| RESponse | |
|---|---|
$_RES |
Always — sent automatically at shutdown |
| _RES | |
|---|---|
| $_RES['status_code'] | number |
| $_RES['error'] | string |
| $_RES['data'] | any |
| $_RES['headers'] | EG: $_RES['headers']['Content-Type'] = 'application/json'; |
don't mix echo
If you're using $_RES avoid using echo (even echo json_encode("me"); will make it go weird) and don't use the header command -- it can give odd results)
$_RES DEFAULTS are only ['error'] and ['data']
Out of the box this
will return this
If you want to extend (EG: to add debug) you need to tell it to by using Inputs::extend('debug');. This is by design.
EG: use this
to get
You will have to have added the line Inputs::extend('debug'); you can then use it
$_RES WARNING
If you don't call exit; and the script continues running (e.g., later logic or another $_RES['data'] overwrite), the final values of $_RES at the end of execution are what gets sent.
You control exactly when to respond by calling exit; after setting $_RES. That is the right and recommended usage pattern for this design.
Install via Composer
$_PATH
$_PATH if you've inserted and id or perhaps uid in the url you can get it but you need to provide the input mask. More complicated set ups are not supported at the moment - terminal params only on index.php
$_HEADER
Access all request headers:
$_PUT, $_PATCH, $_DELETE
These are automatically available when the request method matches:
Example PUT /api/profile with body:
it is safe to test/bail on error like this if (Inputs::is('PUT')){
$_JSON
Always available if request has Content-Type: application/json.
Even for POST, PATCH, etc., $_JSON will work as long as the input is JSON.
it is safe to test if (Inputs::is('JSON')){
$_RES (Auto-Sent Response)
$_RES is automatically sent when the script finishes — no need to manually echo or http_response_code().
License
MIT