Returning API Output to its Rightful Place
Most everyone knows I’m not a Rails guy, and that’s probably never going to change, much to many of my colleague’s dismay.
I do, however, appreciate many of the features of Rails. The one on which I’m going to focus today is its built-in ability to show output in various formats.
My beloved framework, CodeIgniter, doesn’t have this capability built in, and in my development travels, I’ve handled API output in a variety of ways. All of them work just fine, but they really didn’t conform to the MVC architecture. API output, at least how I see it, should be sent to the browser through a view, just like any other sort of output.
A week or so ago, I created a helper that handled all the necessary API output stuff, including converting to JSON or XML. It worked just fine, but I found myself having to refer to the syntax a lot, every time I wanted to use it. It was just too cumbersome. And at about the same time, I began thinking how nice it’d be to use the familiar $this->load->view(); view loader method for APIs, too.
So, that’s just what I did. I took some of the logic from the helper and piled it into a one-size-fits-all view for APIs. It’s a little odd at first, when you look it it, since there’s a whole lot of code, which you don’t usually find in a view file, but, considering you never have to touch any of it directly, it works quite nicely.
I’ll post the code later on, but I just share my thoughts about this method of handling API output from CodeIgniter.



