Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an API-centric application (more specifically: for HMVC in CodeIgniter)

Background:

I am working on a web application, that I plan to launch with iPhone and Android versions in the near future, and I have some questions about developing an API for use by the different front-ends.

I Started with This:

I was reading this TutsPlus tutorial, which talks about creating an API-centric web application, meaning that all of your browser calls and smartphone app calls are routed through the API.

What especially got me excited, was this statement in the tutorial from Twitter:

One of the most important architectural changes is that Twitter.com is now a client of our own API. It fetches data from the same endpoints that the mobile site, our apps for iPhone, iPad, Android, and every third-party application use.

The above statement reflects, what I would ideally like to create.

Then I Saw This:

I found this TutsPlus tutorial, which talks about, how to build a REST API for CodeIgniter. This was good, because I am developing my application in CodeIgniter, using the Modular Extensions plugin - HMVC, and I thought it would be perfect for my purposes.

The only thing, that I'm somewhat lost in, is the way Phil Sturgeon seems to suggest to create the API. He suggests to have all of your front-end code modules completed and then create an API for each of the modules in their respective directories (not necessarily in that order). This would be fine if I didn't want it to be API-centric.

And I Want to Know How to Do This:

How should I proceed to get a blend of both tutorials:

  • API-centric application that can be used by a browser application as well as separate mobile applications
  • REST API for CodeIgniter (with HMVC modules)

Should I:

  • Just follow the API-centric tutorial and try to tailor it to my needs?
  • Follow the CodeIgniter-specific tutorial and use Phil Sturgeon's advice for how to implement it with HVMC?
  • A combination of both?
  • Neither?
like image 814
ServAce85 Avatar asked May 25 '12 16:05

ServAce85


1 Answers

Take a look at Eating your own Dogfood by Nick Jackson. These are slides from CICONF 2012 where he details how this all works.

Building your API first is a great idea, then your application can use the API to fetch the data itself. This is of course a little slower as you're adding a HTTP request on data, but locally its not too bad and if you use HMVC to make the request it's even quicker.

In the tutorial I didn't get into HMVC because it's an extra subject, but it can be awesome for adding modular API's. In some simple applications I just make a /api folder like the tutorial suggested but PyroCMS Professional has an API module and a sexy route to allow modular API stuff:

$route['api/([a-zA-Z0-9_-]+)/(:any)']       = '$1/api/$2';
$route['api/([a-zA-Z0-9_-]+)']              = '$1/api/index';

This means I can have a "api.php" controller in every module and still use the /api/controller/method syntax or even /api/controller if you have function index_get().

I would strongly suggest you grab PyroCMS Professional for this feature. Yep you gotta pay, but it handles user key generation, authorisation, logging, etc all out of the box and handles your website too.

like image 192
Phil Sturgeon Avatar answered Oct 05 '22 22:10

Phil Sturgeon