PHP code example of yidas / codeigniter-rest

1. Go to this page and download the library: Download yidas/codeigniter-rest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yidas / codeigniter-rest example snippets


class ApiController extends yidas\rest\Controller
{
    public function index()
    {
        return $this->response->json(['bar'=>'foo']);
    }
}

public function store($requestData=null) {

    $this->db->insert('mytable', $requestData);
    $id = $this->db->insert_id();
    
    return $this->response->json(['id'=>$id], 201);
}

try {
    throw new Exception("API forbidden", 403);
} catch (\Exception $e) {
    // Pack data into a standard format
    $data = $this->pack(['bar'=>'foo'], $e->getCode(), $e->getMessage());
    return $this->response->json($data, $e->getCode());
}


$config['composer_autoload'] = TRUE;

class Resource extends yidas\rest\Controller {}

$route['resource_name'] = '[Controller]/route';
$route['resource_name/(:any)'] = '[Controller]/route/$1';

public function index() {}
protected function store($requestData=null) {}
protected function show($resourceID) {}
protected function update($resourceID=null, $requestData=null) {}
protected function delete($resourceID=null, $requestData=null) {}

protected $routes = [
    'index' => 'index',
    'store' => 'store',
    'show' => 'show',
    'update' => 'update',
    'delete' => 'delete',
];

class ApiController extends yidas\rest\Controller {

    protected $routes = [
        'index' => 'find',
        'store' => 'save',
        'show' => 'display',
        'update' => 'edit',
        'delete' => 'destory',
    ];
}

protected boolean _setBehavior(string $action, callable $function)

class BaseRestController extends \yidas\rest\Controller
{
    function __construct() 
    {
        parent::__construct();
    
        // Load your Auth library for verification
        $this->load->library('Auth');
        $this->auth->verify('read');
        
        // Set each action for own permission verification
        $this->_setBehavior('store', function() {
            $this->auth->verify('create');
        });
        $this->_setBehavior('update', function() {
            $this->auth->verify('update');
        });
        $this->_setBehavior('delete', function() {
            $this->auth->verify('delete');
        });
    }
    // ...

protected array pack(array|mixed $data, integer $statusCode=200, string $message=null)

$data = $this->pack(['bar'=>'foo'], 403, 'Forbidden');
return $this->response->json($data, 403);

public string getRawBody()

// Request with `application/json` raw
$data = json_decode($this->request->getRawBody);

public array getAuthCredentialsWithBasic()

list($username, $password) = $this->request->getAuthCredentialsWithBasic();

public string getAuthCredentialsWithBearer()

$b64token = $this->request->getAuthCredentialsWithBearer();

public void json(array|mixed $data, integer $statusCode=null)

$this->response->json(['bar'=>'foo'], 201);

public self setFormat(string $format)

$this->response->setFormat(\yidas\http\Response::FORMAT_JSON);

public self setData(mixed $data)

$this->response->setData(['foo'=>'bar']);

public void send()

$this->response->send();

public self withAddedHeader(string $name, string $value)

return $this->response
    ->withAddedHeader('Access-Control-Allow-Origin', '*')
    ->withAddedHeader('X-Frame-Options', 'deny')
    ->json(['bar'=>'foo']);