Create Fake REST APIs Server in CodeIgniter 4

What is CodeIgniter Fake JSON REST APIs Server

How to Install Setup

Clone github repository

$ git clone https://github.com/owthub/codeigniter-4-fake-json-rest-apis-server-using-json-file.git
Cloning into 'codeigniter-4-fake-json-rest-apis-server-using-json-file'... remote: Enumerating objects: 3061, done. remote: Counting objects: 100% (3061/3061), done. remote: Compressing objects: 100% (2048/2048), done. remote: Total 3061 (delta 920), reused 3061 (delta 920), pack-reused 0 Receiving objects: 100% (3061/3061), 5.28 MiB | 468.00 KiB/s, done. Resolving deltas: 100% (920/920), done.
$ php spark serve
# app.baseURL = 'SET-YOUR-APPLICATION-URL'
app.baseURL = 'http://localhost/codeigniter-4-fake-json-rest-apis-server-using-json-file'
http://localhost/codeigniter-4-fake-json-rest-apis-server-using-json-file

Create db.json File

How To Create

{ "students": [ ], "posts": [ ], "comments": [ ] }
{ "students": [ { "id": 1, "name": "Sanjay Kumar", "email": "sanjay@gmail.com" }, { "id": 2, "name": "Vijay Rohila", "email": "vijay@gmail.com" }, { "id": 3, "name": "Dhananjay Negi", "email": "dhananjay@gmail.com" }, { "id": 4, "name": "Ashish Kumar", "email": "ashish@gmail.com" }, ... ], "posts": [ { "id": 1, "title": "Sample Post 1", "description": "This is sample post 1" }, { "id": 2, "title": "Sample Post 2", "description": "This is sample post 2" }, ... ], "comments": [ { "id": 1, "title": "Sample comment 1", "description": "This is sample comment 1" }, { "id": 2, "title": "Sample comment 2", "description": "This is sample comment 2" }, ... ] }
  • URL/students — List all data
  • URL/posts
  • URL/comments
  • URL/students/{studentId} OR URL/students?id={studentId} — List single data
  • URL/posts/{postId} OR URL/posts?id={postId}
  • URL/comments/{commentId} OR URL/comments?id={commentId}
  • A Student have many posts
  • A Post have many comments
{singularResourceName}Id
studentId, postId
{ "students":[ { "id": 1, "name": "Sanjay Kumar", "email": "sanjay@gmail.com" }, ... ], "posts":[ { "id": 1, "studentId": 1, "title": "Sample post 1", "description": "This is sample post 1" }, { "id": 2, "studentId": 1, "title": "Sample post 2", "description": "This is sample post 2" }, { "id": 3, "studentId": 2, "title": "Sample post 3", "description": "This is sample post 3" } ... ], "comments":[ { "id": 1, "postId": 1, "title": "Sample comment 1", "description": "This is sample comment 1" }, { "id": 2, "postId": 1, "title": "Sample comment 2", "description": "This is sample comment 2" }, ... ] }
  • URL/students/{studentId}/posts — List all posts by student id
  • URL/students/posts?studentId={studentId}
  • URL/posts/{postId}/comments — List all comments by post id
  • URL/posts/comments?postId={postId}

Final Words

{ "students": [], "posts": [], ... }{ "students": [ { "id": 1, "name": "Sample" }, { "id": 2, "name": "Sample" } ], ... }
  • Relationship between resources as “id” for their tables and {singular}Id as for others to connect. Like “id” inside students, but for posts it will be “studentId” which connects two resources students and posts.
  • db.json file should placed inside /public/json folder.
  • Resources names should be plural.
  • When you execute POST, PUT/PATCH, DELETE request with fake apis, they will execute even they will return a perfect json output but they don’t do anything at server. Means they don’t save, update or delete anything in real.

--

--

--

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Online Web Tutor

Online Web Tutor

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

More from Medium

Validate Maximum of 10 Numbers in Textbox Using jQuery

XAMPP — MySQL shutdown unexpectedly

How to Create login, register, and logout manually in laravel.

Fastest WordPress hosting service provider is HERE!