A starter source code for a handy PHP restful API development.
This project created mainly for myself - to learn and practice PHP OOP.
- PHP: >=7.4
- MySQL: >=5.7
- Composer
ThingEngineer/PHP-MySQLi-Database-Class
MysqliDb is MySQLi wrapper and object mapper with prepared statements.
steampixel/simplePHPRouter
Simple and small single class PHP router that can handle the whole URL routing.
Respect/Validation
"The most awesome validation engine ever created for PHP!"
firebase/php-jwt
A simple library to encode and decode JSON Web Tokens (JWT) in PHP.
vlucas/phpdotenv
Loads environment variables from .env
to getenv()
, $_ENV
and $_SERVER
automagically.
- JWT Token Authentication
- JWT Refresh Tokens
- Users management
- Role-based routes protection (user, admin)
- More to come...
-
Clone repository:
git clone https://github.com/zikju/php-api-starter
-
Install composer dependencies:
composer install
-
Import file
database.sql
into your MySQL database -
Edit file .env.example.
Change MySQL logins variables to match your own database settings:
DB_HOST = localhost
DB_PORT = 3306
DB_DATABASE = database_name
DB_USERNAME = root
DB_PASSWORD =
-
Rename file .env.example to .env
-
(optional) For easiest way to test endpoints - import file
POSTMAN_ENDPOINTS.json
into your Postman workflow.
After file import - find Collection variables and changeAPI_URL
to your project url.
Method | Endpoint | Parameters | Description |
---|---|---|---|
POST |
/auth/login |
email string requiredpassword string required |
login user |
GET |
/auth/logout |
logout user | |
GET |
/auth/refresh-token |
refresh token |
All RESTful API endpoints below requires a Authorization: Bearer xxxx
header set on the HTTP request.
xxxx is replaced with token generated from the /auth/login
endpoint above.
Method | Endpoint | Parameters | Description |
---|---|---|---|
POST |
/users |
email string - requiredpassword string - requiredrole stringstatus stringnotes string |
Create new user |
GET |
/users/:id |
:id integer - required |
Get user information |
DELETE |
/users/:id |
:id integer - required |
Delete user |
PUT |
/users/:id/edit |
:id integer - requiredrole stringstatus stringnotes string |
Edit user common data |
PUT |
/users/:id/edit/email |
:id integer - requiredemail string - required |
Change user email |