You are viewing...

PHP - Client -> API model

Updated on February 25, 2012 at the 06th hour
Posted under:

DISCLAIMER: Expressed views on this blog are my own.

I recommend throwing out your current model of web programming and use a Client - API model. I personally think this is a better way of handling requests. Basically, you code PHP and it implements a REST API that serves data in whatever format you'd like (e.g. JSON, XML). Once you've got this part down, you can leave the templating to client-side Javascript. PHP now takes on responsibility of serving data to the client.

Why do I recommend using client-side JS for templating? You will be using the client's browser power to generate html instead of your own. You free up bandwidth and compute power for other responses or things you want to do. Example, let's say you wanted to create a list on the page, well leave it for js! Let JS and CSS handle the templating for you. PHP sends the list data in a JSON format, we use JS to read that list and format it into html and attach whatever classes we need, then use CSS to style it up. Benefit: js files are cached! No need to keep re-sending html.

Serving data to the client and leaving templating as a client responsibility makes sense especially when you want to create a mobile application. You'll design your mobile app with your API in mind. Your API will probably have to detect if a mobile browser is requesting data or if the request contains some type of header indicating the need for data for a mobile app, then you can send the data that you need to send.

Am I supposed to show you a how to? If you understand this, go implement it now!
You just read "PHP - Client -> API model". Please share if you liked it!
You can read more recent posts here.