PHP code example of chriskacerguis / codeigniter-restserver

1. Go to this page and download the library: Download chriskacerguis/codeigniter-restserver 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/ */

    

chriskacerguis / codeigniter-restserver example snippets


use chriskacerguis\RestServer\RestController;

class Example extends RestController


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



use chriskacerguis\RestServer\RestController;

class MY_REST_Controller extends RestController
{
    public function __construct()
    {
        parent::__construct();
        // This can be the library's chriskacerguis\RestServer\Format
        // or your own custom overloaded Format class (see bellow)
        $this->format = new Format();
    }
}



use chriskacerguis\RestServer\Format as RestServerFormat;

class Format extends RestServerFormat
{
    public function to_pdf($data = null)
    {
        if ($data === null && func_num_args() === 0) {
            $data = $this->_data;
        }

        if (is_array($data) || substr($data, 0, 4) != '%PDF') {
            $html = $this->to_html($data);

            // Use your PDF lib of choice. For example mpdf
            $mpdf = new \Mpdf\Mpdf();
            $mpdf->WriteHTML($html);
            return $mpdf->Output('', 'S');
        }

        return $data;
    }
}