# Technical Documentation: chriskacerguis/codeigniter-restserver > ℹ️ **Provenance:** Hybrid Fusion: `chriskacerguis/codeigniter-restserver` (README + 1 In-Tree Chapters) · [CodeWiki Reference](https://codewiki.google/github.com/chriskacerguis/codeigniter-restserver) · Recency: Active (< 180 days) ## 1. Project Overview & Quickstart (chriskacerguis/codeigniter-restserver) # CodeIgniter RestServer A fully RESTful server implementation for CodeIgniter 3 using one library, one config file and one controller. > [!IMPORTANT] > I have published the first "beta" of codeigniter-restserver 4. See the "development" branch. Please be sure to note the system requirments. ## Requirements - PHP 7.2 or greater - CodeIgniter 3.1.11+ ## Installation ```sh composer require chriskacerguis/codeigniter-restserver ``` ## Usage CodeIgniter Rest Server is available on [Packagist](https://packagist.org/packages/chriskacerguis/codeigniter-restserver) (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: ```json "chriskacerguis/codeigniter-restserver": "^3.1" ``` or run ```sh 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) ```php use chriskacerguis\RestServer\RestController; ``` Step 2: Extend your controller ```php 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 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 ); } } } } ``` ## Extending supported formats If you need to be able to support more formats for replies, you can extend the `Format` class to add the required `to_...` methods 1. Extend the `RestController` class (in `libraries/MY_REST_Controller.php`) ```php format = new Format(); } } ``` 2. Extend the `Format` class (can be created as a CodeIgniter library in `libraries/Format.php`). Following is an example to add support for PDF output ```php _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; } } ``` ## 2. In-Tree Documentation Chapters (chriskacerguis/codeigniter-restserver) # CodeIgniter RestServer A fully RESTful server implementation for CodeIgniter 3 using one library, one config file and one controller. > [!IMPORTANT] > I have published the first "beta" of codeigniter-restserver 4. See the "development" branch. Please be sure to note the system requirments. ## Requirements - PHP 7.2 or greater - CodeIgniter 3.1.11+ ## Installation ```sh composer require chriskacerguis/codeigniter-restserver ``` ## Usage CodeIgniter Rest Server is available on [Packagist](https://packagist.org/packages/chriskacerguis/codeigniter-restserver) (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: ```json "chriskacerguis/codeigniter-restserver": "^3.1" ``` or run ```sh 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) ```php use chriskacerguis\RestServer\RestController; ``` Step 2: Extend your controller ```php 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 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 ); } } } } ``` ## Extending supported formats If you need to be able to support more formats for replies, you can extend the `Format` class to add the required `to_...` methods 1. Extend the `RestController` class (in `libraries/MY_REST_Controller.php`) ```php format = new Format(); } } ``` 2. Extend the `Format` class (can be created as a CodeIgniter library in `libraries/Format.php`). Following is an example to add support for PDF output ```php _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; } } ``` --- METRICS --- - Files Extracted: 2 - Estimated Token Budget: ~2174 tokens - Recency Window: Active (< 180 days) - Canonical Reference: https://codewiki.google/github.com/chriskacerguis/codeigniter-restserver