Skip to content

A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.

License

Notifications You must be signed in to change notification settings

chriskacerguis/codeigniter-restserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeIgniter RestServer

StyleCI

A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.

Requirements

  • PHP 7.2 or greater
  • CodeIgniter 3.1.11+

Installation

composer require chriskacerguis/ci-restserver

Usage

CodeIgniter Rest Server is available on Packagist (using semantic versioning), and installation via composer is the recommended way to install Codeigniter Rest Server. Just add this line to your composer.json file:

"chriskacerguis/codeigniter-restserver": "^3.1"

or run

composer require chriskacerguis/codeigniter-restserver

Step 1: Add this to your controller (should be before any of your code)

use chriskacerguis\RestServer\RestController;

Step 2: Extend your controller

class Example extends RestController

Basic GET example

Here is a basic example of

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

use chriskacerguis\RestServer\RestController;

class Example extends REST_Controller {

    function __construct()
    {
        // Construct the parent class
        parent::__construct();
    }

    public function users_get()
    {
        // Users from a data store e.g. database
        $users = [
            ['id' => 1, 'name' => 'John', 'email' => '[email protected]'],
            ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]'],
        ];

        $id = $this->get('id');

        if ($id === null)
        {
            // Check if the users data store contains users
            if ($users)
            {
                // Set the response and exit
                $this->response($users, 200);
            }
            else
            {
                // Set the response and exit
                $this->response([
                    'status' => false,
                    'message' => 'No users were found'
                ], 404);
            }
        }
    }
}

About

A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.

Resources

License

Stars

Watchers

Forks

Packages

No packages published