See how the exact same Medium.com clone (called CodeIgniter4 Realworld API). This codebase was created to demonstrate a fully functional REST API built with CodeIgniter4, including CRUD operations, authentication, routing, pagination, and more.
Hope you'll find this example helpful. Pull requests are welcome!
Please check the official CodeIgniter4 installation guide for server requirements before you start. Official Documentation
Clone the repository
git clone https://github.com/agungsugiarto/codeigniter4-realworld-api.git
Switch to the repo folder
cd codeigniter4-realworld-api
Install all the dependencies using composer
composer install
Copy the example env file and make the required configuration changes in the .env file
cp .env.example .env
Run the database migrations (Set the database connection in .env before migrating)
php spark migrate
Start the local development server
php spark serve
You can now access the server at http://localhost:8080
TL;DR command list
git https://github.com/agungsugiarto/codeigniter4-realworld-api.git
cd codeigniter4-realworld-api
composer install
cp .env.example .env
Make sure you set the correct database connection information before running the migrations Environment variables
php spark migrate
php spark serve
Populate the database with seed data with relationships which includes users, articles, comments, tags, favorites and follows. This can help you to quickly start testing the api or couple a frontend and start using it with ready content.
Run the database seeder and you're done
php spark db:seed DatabaseSeeder
Note : It's recommended to have a clean database before seeding. You can refresh your migrations at any point to clean the database by running the following command
php spark migrate:refresh
- codeigniter4-authentication - For authentication
- codeigniter4-authentication-jwt - For authentication using jwt.
- codeigniter4-cors - For handling Cross-Origin Resource Sharing (CORS)
- codeigniter4-repository - For Implementation of repository pattern for CodeIgniter 4
- league/fractal - For provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON
app/Config
- Contains all the application configuration filesapp/Controllers
- Contains all the api controllersapp/Criteria
- Contains all the criteria are a way to build up specific query conditionsapp/Database/Migrations
- Contains all the database migrationsapp/Database/Seeds
- Contains the database seederapp/Entities
- Contains all the classes as a first-class citizen in it’s database layerapp/Filters
- Contains the auth personal access token filterapp/Models
- Contains all the modelsapp/Repository
- Contains all the repository objectapp/Scopes
- Contains all the repository define which fields can be used to scope your queries by setting $searchable propertyapp/Transformers
- Contains all transformer output JSONroutes
- Contains all the api routes defined inapi.php
file
.env
- Environment variables can be set in this file
Note : You can quickly set the database information and other variables in this file and have the application fully working.
Run the CodeIgniter4 development server
php spark serve
The api can now be accessed at
http://localhost:8080/api
Request headers
Required | Key | Value |
---|---|---|
Yes | Content-Type | application/json |
Yes | X-Requested-With | XMLHttpRequest |
Optional | Authorization | Token {JWT} |
Refer the api specification for more info.
This applications uses JSON Web Token (JWT) to handle authentication. The token is passed with each request using the Authorization
header with Token
scheme. The JWT authentication filter handles the validation and authentication of the token. Please check the following sources to learn more about JWT.
- https://jwt.io/introduction/
- https://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
- https://github.com/agungsugiarto/codeigniter4-authentication
- https://github.com/agungsugiarto/codeigniter4-authentication-jwt
This applications has CORS enabled by default on all API endpoints. The CORS allowed origins can be changed by setting them in the config file. Please check the following sources to learn more about CORS.
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
- https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
- https://www.w3.org/TR/cors
This project is free software distributed under the terms of the MIT license.