CodeIgniter Core Custom by Ngthuc

The CodeIgniter framework with core custom

Server Requirements

Installation

Create project

  1. Run composer create project (for init new project) or update (for reload dependencies on new environment) via command line at root folder
    • Create project
      composer create-project ngthuc/ci-composer
      
    • Reload dependencies
      composer update
      
  2. Create .env file same [.]env to set-up database

    Require project into another project

  3. Run composer require project (for init new project) or update (for reload dependencies on new environment) via command line at root folder
    • Require project
      composer require ngthuc/ci-composer
      
    • Reload dependencies
      composer update
      
  4. Move all files from /vendor/ngthuc/ci-composer/app/ to the root folder structure:
    root                          # → Root Directory
    └── vendor/
     └── ngthuc/
         └── ci-composer/
             └── app/
                 ├── apllication/
                 ├── public/
                 ├── .bowerrc
                 ├── .editorconfig
                 ├── .gitignore
                 ├── .htaccess
                 ├── [.]env
                 ├── bower.json
                 ├── index.php
                 └── license.txt
    
  5. Create .env file same [.]env to set-up database

Usage

Usage RestAPI

CodeIgniter Rest Server is available on package.

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:

<?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' => 'john@example.com'],
            ['id' => 1, 'name' => 'Jim', 'email' => 'jim@example.com'],
        ];

        $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 );
            }
        }
    }
}

Author

Credit

This project using projects or dependencies:

License

CodeIgniter Core Custom is licensed under MIT License.