This sandbox is for people who would like to build web REST API's with token based structure like JWT using PHP. Its goal is to enable you to develop API much faster than you could if you were writing code from scratch, by providing a template for your workings with the REST API with JWT based tokens.
- Complete REST API control
- JWT based access tokens
- CRUD operations
- Register/Login Mechanism
- Proper Authentication
- Validation control
- DB structure given
- Required SQL given
- Routing handled
- Postman collection added
- Download postman collection and test this sandbox now.
- Change
jwt_key
&token_expire_time
according to your need. - Change
$config['base_url']
in config.php - Change DB credentials accordingly in database.php
- Import .sql file of create by database tables from raw SQL given above controller file.
- Register a User.
- Login with that user to get the
access_token
. - To perform crud operations you have to supply the
access_token
in header forJwt-Authorization
param with other data in body section. - Initially
access_token
expires after 10 minutes. jwt_key
must be changed for your own protection in production environment.
Verb POST
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/register
Body |
---|
username |
password |
Verb POST
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/login
Body |
---|
username |
password |
Verb POST
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/api/test_api/verify_token
Headers | Body |
---|---|
Jwt-Authorization |
Verb GET
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/product/list
Headers | Body |
---|---|
Jwt-Authorization |
Verb GET
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/product/show/:id
Headers | Body |
---|---|
Jwt-Authorization |
Verb POST
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/product/insert
Headers | Body |
---|---|
Jwt-Authorization | name |
price |
Verb POST
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/product/update/:id
* Ideally we should use PUT verb to UPDATE action. But PHP doesn’t work properly with PUT (it's hard to get the PUT parameters). So the solution is to use POST verb to perform an UPDATE.
Headers | Body |
---|---|
Jwt-Authorization | name |
price |
Verb DELETE
URL http://localhost/dev/php-codeigniter3/rest_api_jwt/product/delete/:id
Headers | Body |
---|---|
Jwt-Authorization |
- Tested in XAMPP 3.3.0 with:
- PHP 8.0.13
- 10.4.22-MariaDB
- Apache/2.4.51 (Win64)
- This should work with PHP 5.6 or newer.
This sandbox is based on these repos: