- In this project I'll be trying to create an
CRUD-APIwith vanillaPHP - To both strenthen my knowledge in
PHPand to understand howRouteswork inLARAVEL
-
All the styles are kept in the
/stylesfolder mostlySCSS -
The logic handling the
ROUTESandRESPONSESare found in./control.php -
Disclaimer: The data indata/data.jsonwas gotten fromhttps://jsonplaceholder.typicode.com/ -
Disclaimer: The data indata/dogfacts.jsonwas gotten fromhttps://jsonplaceholder.typicode.com/ -
The
ROUTEhandler now accepts an extra parameter to know the number of data to return -
API now sends returns data
-
Added a 404 page for invalid routes
-
receiver.phpwill receive and handlePOSTrequests -
receiver.phpreturns a response to the clinet(the browser that sent the post) -
POSTRequests now works whenJSONdata is sent -
We use
php://inputinstead of$_POSTto fetch the data sent because$_POSTchecks for data sent fromformsor with headerContent-Type: application/x-www-form-urlencodedormultipart/form-datawhilephp://inputchecks for anyPOSTdata regardless of it'scontent-type -
A
.envfile is used to store sensitive data -
To use a
.envfile inPHPfirst create a file named.env -
Add the credentials you want to keep hidden like
API_KEY=1234 -
Make sure you add
.envto your.gitignorefile -
Then run
composer require vlucas/phpdotenv
- Require the library
require_once __DIR__ . "/vendor/autoload.php"
-
For more info about
vlucas/phpdotenvread their documentation -
For a better example of
phpdotusage checkout walternomas -
CaveKey (API key) validation now in place to stop any nupermitted
POSTrequest
- It retuns data when a
GETrequest is sent - Accepts data when
POSTrequest is sent and with validation - It's an
API