CodeIgniter Forums
Output based on the request type - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Output based on the request type (/showthread.php?tid=1631)



Output based on the request type - sintakonte - 03-27-2015

Hey guys,

i could need some advice.
I've an extensive application (200+ Models)

Lately we got more and more a situation where we need to output the content in different Types (XML, JSON, HTML, CSV, PDF and so on).
Our solution till now is like the following:

Extend the affected controller with a function json (e.g. for a json request) -> pick up the data from the model and send the data back
An Url looks like: /news/list/json or /news/list/xml and so on.
In this situation you can decide what you want.

The big downside is the redundancy - always put a function json in the controller method and so on.

My idea now is, to create a service like REST but with more options - which means i need to react based on the clients request.
This means, i need different base view folders like (views/xml/ etc.)
I don't think i have a problem to accomplish this from a technical point of view (extending the loader class and so on)

But does anybody know a fool proof method to determine such a request (maybe via content-type headers ?) or is my idea totally nuts ? Wink
Or are there any other solutions around ?

thx in advance for your answers


RE: Output based on the request type - Narf - 03-27-2015

Content-Type negotiation (via the Accept request header) is the standard and most fool-proof way to do this.


RE: Output based on the request type - silentium - 03-30-2015

If you're planing on doing a REST API, I can recommend this REST Server library that might help you with some stuff. https://github.com/chriskacerguis/codeigniter-restserver


RE: Output based on the request type - sintakonte - 03-31-2015

thx for your help guys
it looks like - this is something what i was looking for