- In this project I'll be trying to create an
CRUD-API
with vanillaPHP
- To both strenthen my knowledge in
PHP
and to understand howRoutes
work inLARAVEL
-
All the styles are kept in the
/styles
folder mostlySCSS
-
The logic handling the
ROUTES
andRESPONSES
are found in./control.php
-
Disclaimer
: The data indata/data.json
was gotten fromhttps://jsonplaceholder.typicode.com/
-
Disclaimer
: The data indata/dogfacts.json
was gotten fromhttps://jsonplaceholder.typicode.com/
-
The
ROUTE
handler 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.php
will receive and handlePOST
requests -
receiver.php
returns a response to the clinet(the browser that sent the post) -
POST
Requests now works whenJSON
data is sent -
We use
php://input
instead of$_POST
to fetch the data sent because$_POST
checks for data sent fromforms
or with headerContent-Type: application/x-www-form-urlencoded
ormultipart/form-data
whilephp://input
checks for anyPOST
data regardless of it'scontent-type
-
A
.env
file is used to store sensitive data -
To use a
.env
file inPHP
first create a file named.env
-
Add the credentials you want to keep hidden like
API_KEY=1234
-
Make sure you add
.env
to your.gitignore
file -
Then run
composer require vlucas/phpdotenv
- Require the library
require_once __DIR__ . "/vendor/autoload.php"
-
For more info about
vlucas/phpdotenv
read their documentation -
For a better example of
phpdot
usage checkout walternomas -
CaveKey (API key) validation now in place to stop any nupermitted
POST
request
- It retuns data when a
GET
request is sent - Accepts data when
POST
request is sent and with validation - It's an
API