Codeigniter Rest API

Codeigniter is lightweight mvc framework. A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller. I’m using following git repository to create api

https://github.com/chriskacerguis/codeigniter-restserver

first you need to download fresh latest copy of codeigniter via codeigniter.com then you have to install the repostiory file to our codeigniter project. I’m using composer to do the process as follows.

composer require chriskacerguis/codeigniter-restserver or just add this line to composer.json file in our project directory "chriskacerguis/codeigniter-restserver": "^3.0" or you can dowwnload manually to your project as your desire.
now you have to update and add the following directory files from the rest-api repository
app.dev\application\config\rest.php
app.dev\applications\libraries\REST_Controller.php

now we need to create new controller. in my case i’m create Articles.php file in controllers
and put following code snippet

use Restserver\Libraries\REST_Controller;

 class Articles extends Restserver\Libraries\REST_Controller {

 public function __construct() {
 parent::__construct();
 header("Access-Control-Allow-Origin: *");
}

 public function addArticle_post() {
 //put insert query or model init
 $this->response($_POST);
 }

}

please note that in here i’m create function addArticle_post means it use post method. now we need to update our route file in app.dev\application\config\routes.php using following snippet

//api routes
$route['article/add'] = 'Articles/addArticle';

now we can check our api url by using postman.

we can use several types of response formats

api/example/users
api/example/users/format/csv
api/example/users/id/1
api/example/users/1
api/example/users/id/1.xml
api/example/users/id/1/format/xml
api/example/users/id/1?format=xml
api/example/users/1.xml
api/example/users/format/json
api/example/users.html
api/example/users/format/html
api/example/users?format=html

0 0 vote
Article Rating

by kushan


Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
lasitha
lasitha
2 years ago

i got an error while using this repo