PHP – Create a REST API with CodeIgniter

Introduction

CodeIgniter is a fairly simple framework for PHP. Compared to his big brothers such as Zend or YII, he has the advantage of being small and very simple to take in hand. For me, currently the best for small projects such as creating a REST-API.

Installation

To start, go download the official version on github https://github.com/philsturgeon/codeigniter-restserver

Selection_081

 

Deziper everything has the right place. Then go into the application/config/config.php file and indicate the baseurl

$config['base_url'] - 'http://api.local';

this is of course the URL of your API.

The Urls

If the server is correctly configured by calling the base_url, the default page will appear

Selection_082

Htaccess

To not have the index.php in the url, you need to write a .htaccess and place it at the root of your site.

RewriteEngine On
RewriteCond %-REQUEST_FILENAME!-f
RewriteCond %-REQUEST_FILENAME!-d
RewriteRule $$/index.php/$1

Controller

Publicités

Then you can check the folder /controllers/api or you will find the controller example.php. By analyzing it you can guess that the minimum for your controller is:

response(array('error' => 'test'), 404);
   }
}
?>

to see the result, just call api.local/api/example/test


   test

Publicités

Leave a Reply