Coding a simple Web API with CodeIgniter – Part 1

codeigniter_logoCodeIgniter is an easy to use php framework with a very straightforward documentation.

In this series of articles I’ll explain how to setup a simple Web API that will allow us to retrieve table metadata from tables in a MySQL server and perform CRUD operations against them. This API will make work the Ext jS dynamic grid system we implemented in a recent post.

Note that this is only a use case, once you get the basics you will see how easy is to implement your own functionality in a well-organized API.

You can find all the code in this GitHub repository.

For this example we will be using apache with the mod_rewrite module, which will be normally enabled by default in any LAMP stack. mod_rewrite is a rewrite engine that will let us change URL signatures in the fly upon some rules that we specify. So for example, we may have that the URL to access the php file that contains some method of our API is something like http://www.mysite.com/php/index.php/db/create, we can make it so that any access will remove the index.php from the URL leaving us with  a more friendly http://www.mysite.com/php/db/create. Fortunately we will only have to specify this behavior once using a file called .htaccess, which will let us tinker with apache in many ways.

The first step is to download the CodeIgniter zip and decide your folder structure. My approach is to create a php and lib/CodeIgniter folders in the site root and make the following operations:

  • Copy the contents of the application folder of the zip file inside the php folder.
  • Copy the contents of the system folder of the zip file inside the lib/CodeIgniter folder.
  • Copy index.php of the root folder of the zip inside the php folder.

If you decide to follow my structure it should look like this.

Next we have to tell CodeIgniter about the folder locations.

Edit php/index.php and change $system_path and $application_folder to match your structure. In my case it would be:

You will notice that if you try to access php/ in your web browser you’ll get a 403.

For now edit the .htaccess file inside the php folder and delete its content, you should now be able to access php/, if everything went fine a CodeIgniter welcome page should greet you.

CodeIgniter is a MVC framework, but for this example we will just rely on its controllers. CodeIgniter controllers will allow us to place our API methods inside them making use of an expressive URL where you can map URL segments (URIS) to entities and actions.

Let’s show this by faking our example API. Create a file called dbFake.php inside php/controllers/ and place the following code.

Now let’s call our fake methods, we can reach them with a URL that has the following structure:

http://mysite.com/php/index.php/controllerName/methodName

A call to the create method inside the db controller would look like this:

http://mysite.com/php/index.php/db/create

This maps quite nice with the method intention; we have a database resource (db) and we want to perform a create action against it. We could have more concrete resources and we may insert additional levels of organization creating subfolders inside the controller folder, so for example, if you are implementing a file repository it would make sense to have methods mapped as php/repository/file/upload and php/repository/folder/rename.

Now let’s get rid of the index.php segment. Edit the .htaccess file inside php/, paste the following and change /site_folder/ accordingly.

Also edit php/config/config.php, modify $config['base_url'] to match your base URL including the path that you specified in the $application_folder options when you edited php/index.php.

In my case looks like

$config['base_url'] = 'http://localhost/blog/p2-ciapi/php/';

Also delete the index.php string from $config['index_page'] so it looks like

$config['index_page'] = '';

In the next post we will talk into the specifics of CodeIgniter, but for now, I’d recommend you to take a look to the CodeIgniter manual if you haven’t, it’s an easy read.

And by the way, if you are following the dynamic grid in Ext JS post, or you plan to make your API coexist with an Ext JS application, here you have an example web structure that may suit your needs.

 

Acerca de

Ver todas las entradas de

2 Responses

  1. iqbalhabibie dice

    I have make folder php/repository/file/upload and php/repository/folder/rename. But I dont understand for /site_folder/ and i dont know where I put this script:

    RewriteEngine On

    # Put your installation directory here:
    # If your URL is http://www.example.com/, use /
    # If your URL is http://www.example.com/site_folder/, use /site_folder/

    RewriteBase /site_folder/

    # Do not enable rewriting for files or directories that exist
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # For requests that are not actual files or directories,
    # Rewrite to index.php/URL
    RewriteRule ^(.*)$ index.php/$1 [PT,L]