Giter Site home page Giter Site logo

swdreams / codeigniter-restserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chriskacerguis/codeigniter-restserver

2.0 1.0 3.0 3.85 MB

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

License: MIT License

PHP 98.15% HTML 1.85%

codeigniter-restserver's Introduction

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/codeigniter-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

Note that you will need to copy rest.php to your config directory (e.g. application/config)

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. This controller, which should be saved as Api.php, can be called in two ways:

  • http://domain/api/users/ will return the list of all users
  • http://domain/api/users/id/1 will only return information about the user with id = 1
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use chriskacerguis\RestServer\RestController;

class Api extends RestController {

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

    public function users_get()
    {
        // Users from a data store e.g. database
        $users = [
            ['id' => 0, 'name' => 'John', 'email' => '[email protected]'],
            ['id' => 1, '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 );
            }
        }
        else
        {
            if ( array_key_exists( $id, $users ) )
            {
                $this->response( $users[$id], 200 );
            }
            else
            {
                $this->response( [
                    'status' => false,
                    'message' => 'No such user found'
                ], 404 );
            }
        }
    }
}

codeigniter-restserver's People

Contributors

adg-wandu avatar boh1996 avatar c4xp avatar chernjie avatar chriskacerguis avatar ckacerguis avatar cnanney avatar davidnewhall avatar davidstanley01 avatar dfreerksen avatar franksierra avatar grawerpl avatar ivantcholakov avatar jamierumbelow avatar jcbuck avatar jerel avatar juanatgmo avatar natanfelles avatar onema avatar repox avatar ruthlessfish avatar sekati avatar smizdev avatar softwarespot avatar sskylar avatar swas avatar teabrg avatar tjoosten avatar tpojka avatar zaherg avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.