Setup RESTful Service With Codeigniter 3

Introduction

This tutorial will show you how to setup RESTful service with CodeIgniter so that we can create and deploy REST services with CodeIgniter applications. Codeigniter Rest Server is a fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.

Prerequisites

PHP 7.3.5 – 7.4.23, CodeIgniter 3.1.11, Apache 2.4 HTTP Server (Optional)

Download the Rest Server from link https://github.com/chriskacerguis/codeigniter-restserver

Project Directory

It’s assumed that you have setup PHP and CodeIgniter in your system.

Now I will create a project root directory called codeigniter-rest-server-setup.

Now move all the directories and files from CodeIgniter framework into the project root directory.

I may not mention the project root directory in subsequent sections, and I will assume that I am talking with respect to the project root directory.

Rest Server Installation

Extract the downloaded archive directory. Let’s say you have extracted the zip into root folder codeigniter-restserver-master.

  • Copy codeigniter-restserver-master\src\rest.php file to CodeIgniter application’s <project’s root directory>\application\config folder.
  • Copy codeigniter-restserver-master\src\RestController.php file to CodeIgniter application’s <project’s root directory>\application\controllers folder.
  • Copy codeigniter-restserver-master\src\Format.php file to CodeIgniter application’s <project’s root directory>\application\controllers folder.
  • Copy codeigniter-restserver-master\language\english\rest_controller_lang.php file to CodeIgniter application’s <project’s root directory> \application\language\english folder.

REST Controller Class

Let’s say you want to create a REST controller class for building REST API. Then you have to load the RestController.php file and Format.php file along with namespace as shown in the following file – CustomRestController.php.

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

require_once 'Format.php';
require_once 'RestController.php';

use chriskacerguis\RestServer\RestController;

class CustomRestController extends CI_Controller {

	function __construct() {
        parent::__construct();
    }
	
	public function index()	{
		$data['...'] = ...;
		$this->load->view('...', $data);
	}
}

Now you are done setup RESTful service with CodeIgniter.

Now you can follow any example:

Source Code

Download

Leave a Reply

Your email address will not be published.