Hi there, My name is Ilori Stephen Adejuwon and I am a Fullstack Developer (Backend Heavy) and this project demonstrates how to create a REST API using PHP and JWT for authentication. The term REST stands for REPRESENTATIONAL STATE TRANSFER and it is used for transferring data by exposing various endpoints via an HTTP Interface.
Without further ado, If you will like to find more information about this topic, I suggest you read my Article on learning dollars where I took my time to explain the term REST API and it also covers how you can create yours.
- Clone the project
- Open the project in your terminal and run the command
composer install
. This will install all of the project dependencies. - Create your database and upload the sql file in the project's root dir.
- Open the Model Folder and edit the Model.php file. You are editing the protected properties of this class. Replace them with your system environment variables.
- You can now begin creating your own Controllers, Models or Middlewares inside of the already created directory called Controller, Model and Middleware respectively.
- After doing the above instructions, you can open the project in your terminal or cmd and run the command
php -S 127.0.0.1:8000
- Well, your project is up and running now. you can now open Postman and begin consuming endpoints.
By default, these are the list of endpoints available when you clone the project. Your router || api.php
lives inside of the router directory
inside of the App folder
.
- Create New User
/api/v1/user
:HTTP POST
$Klein->respond('POST', '/api/v1/user', [ new UserController(), 'createNewUser' ]);
- Login A User
/api/v1/user-auth
:HTTP POST
$Klein->respond('POST', '/api/v1/user-auth', [ new UserController(), 'login' ]);
- Create New Catalog
/api/v1/catalog
:HTTP POST
$Klein->respond('POST', '/api/v1/catalog', [ new CatalogController(), 'createNewCatalog' ]);
- Updates A Catalog
/api/v1/catalog
:HTTP PUT || HTTP PATCH
$Klein->respond(['PATCH', 'PUT'], '/api/v1/catalog/[:id]', [ new CatalogController(), 'updateCatalog']);
- Fetches A Catalog By Id
/api/v1/fetch-catalog-by-id/[:id]
:HTTP GET || HTTP HEAD
$Klein->respond(['GET', 'HEAD'], '/api/v1/fetch-catalog-by-id/[:id]', [ new CatalogController(), 'fetchCatalogById' ]);
- Fetches A Catalog By Id
/api/v1/fetch-catalog-by-id/[:id]
:HTTP GET || HTTP HEAD
$Klein->respond(['GET', 'HEAD'], '/api/v1/fetch-catalog-by-id/[:id]', [ new CatalogController(), 'fetchCatalogById' ]);
- Deletes A Catalog By It's ID
/api/v1/del-catalog/[:id]
:HTTP DELETE
$Klein->respond('DELETE', '/api/v1/del-catalog/[:id]', [ new CatalogController(), 'deleteCatalog' ]);
- Create A Product
/api/v1/product/
:HTTP POST
$Klein->respond('POST', '/api/v1/product', [ new ProductController(), 'createProduct' ]);
- Updates A Product
/api/v1/product/[:id]
:HTTP POST
$Klein->respond('POST', '/api/v1/product/[:id]', [ new ProductController(), 'updateProduct' ]);
- Get A Product By It's ID
/api/v1/product/[:id]
:HTTP GET || HTTP HEAD
$Klein->respond('GET', '/api/v1/fetch/[:id]', [ new ProductController(), 'getProductById' ]);
- Get A List Of Products
/api/v1/products
:HTTP GET || HTTP HEAD
$Klein->respond('GET', '/api/v1/products', [ new ProductController(), 'fetchProducts' ]);
- Delete A Product By It's ID
/api/v1/products
:HTTP GET || HTTP DELETE
$Klein->respond(['DELETE', 'GET'], '/api/v1/delete-product/[:id]', [ new ProductController(), 'deleteProduct' ]);
Feel free to give this a spin and if you feel like, You can also contribute to the Project. Thank you very much, I remain Ilori Stephen A!