Who am I.Introduce myVBO.About this presentation.What I’ll talk about.
Neil – plugins to consumeNot much code.Overview.All the slides will be online right after lunch.
Raise your hand up if you use CakePHP to handle some kind of data.Now put your hand down if you don’t have an API.If you have info why not be a platform?It doesn’t matter if the platform is very simple. A blog is a platform. It has an API for posting new articles and it has an RSS feed for syndicating them.Now that I simplified it.That’s my take.
Even if you are developing a closed API…I hope everyone considers open APIs.All this roles up into one concept. “The platform play.” So if you need something to go back to your boss or your investors with… that’s the thing. You’re making a platform play.
If this was five years ago...But the strategy works pretty well. TwitterFacebookBit.lyAmazonandSalesforceNow it is almost a necessity to have an API of some sort.
There are multiple patterns for APIs. There are a couple more lesser used ones but the two big ones are REST and RPC.Within those patterns you can use one or more formats to transfer your data.
Rest stands for Representational State Transfer incase you missed it in Neal’s presentation. As mentioned yesterday, the largest example of REST in the wild is HTTP.Luckily for us, CakePHP is usually layered on-top of HTTP so it inherits all the RESTful mechanisms.REST has a concept called resources (a specific user or comment are two examples).They are also called nouns which are acted on by verbs.There are five verbs in HTTP. We will focus on three.Finally, one last important thing… CakePHP makes REST easy.
The app that I will be using as an example today is the simplest app that I could think of.It is a URL shortening services that allows you to authenticate and thus be able to delete and edit URLs that you yourself shortened, and also basic CRUD.
There are two models. The user model, which is pretty standard for a CakePHP project, and the urls model which I have on the screen.A full URL shortened can, of course, get much more complicated than that. But for today I’m keeping it basic.
Once you’ve baked your model and what not you can open up your router and map the resource. This will register all the routes you need for REST in one call.You can still do it manually if you want but you don’t have to.These are the six routes registered when you map a resource.
One rule to live by is to never write or delete data on anything that is not a POST, PUT, or DELETE request.The main purpose of this rule is to protect against Cross Site Request Forgeries or CSRF attacks which are every difficult to defend against otherwise.Say that the add method accepted GET requests. Someone could then simply embed an image on a page with the add URL as a source and execute a add() as any user who visits the site.
Before we begin developing views we’ll haveto tell PHP to recognize file extensions and switch the views and layouts accordingly.We do this by turning on parseExtensions in the routes.php file and including the RequestHandler component in the app_controller.The RequestHandler component is what actually switches the views. It also includes helpers automatically in the view if a helper has the same name as the extension (like XML) and parses incoming POSTed XML and assigns it to the data property of the controller.
We now need to create a couple views.The Json view is the first and the one that I like the most. Because it is simple and easy to understand.It is fast thanks to native PHP support, and also very wide-spread.What you see here is the entire view for the view action in the urls controller.Notice the path to the view. The RequestHandler will tell Cake to look in the json folder for the appropriate view.
Now for the XML view. And I can hear the boos now.XML does have some benefits. It is strongly typed, human readable, and has lots of existing tools available.Like Json, the view is pretty self-explanatory. Note the xml sub-directory in the view path.
One of the best parts about using parseExtensions and RequestHandler is you can literally have as many views as you want into the data.I listed just some of them here.
If you did the ACL stuff Erik was talking about…Little difficult. Default behavior is redirectController, model, and object
1. API Development<br />Becoming the Platform<br />(CakePHP for Back-End Development<br />or Cake for Web Services)<br />By Andrew Curioso<br />CakeFest<br />2010<br />
2. Introduction<br />Yesterday: <br />Designing CakePHPplug-ins for consuming APIs<br />Today:<br />Create your own API<br />Basic setup<br />Extras<br />
3. Become a platform<br />Be “a” platform<br />A blog is a platform<br />
4. Become a platform<br />Internal only (closed)<br />Multi-platform (consumers)<br />Scalable<br />External (open)<br />Everything +<br />Growth<br />Mash-ups!<br />Innovation<br />Evangelists<br />“The Platform Play”<br />
19. Status Codes<br />Success<br />200 OK *<br />201 Created *<br />303 See Other *<br />Error<br />401 Unauthorized *<br />402 Payment Required<br />403 Forbidden *<br />404 Not Found *<br />Error (continued)<br />405 Method Not Allowed *<br />409 Conflict<br />410 Gone<br />500 Internal Server Error<br />501 Not Implemented<br />503 Service Unavailable<br />
20. Add Method<br />If not a POST or PUT request<br />405 Method Not Allowed<br />Already existed<br />303 See Other<br />Save success<br />201 Created<br />Failure<br /> 200 OK with explanation<br />
21. Edit Method<br />If not a POST request<br />405 Method Not Allowed<br />Invalid ID<br />404 File Not Found<br />Success<br />200 OK<br />Failure<br />200 OK with explanation<br />
22. Delete Method<br />If not a POST request<br />405 Method Not Allowed<br />Invalid ID<br />404 File Not Found<br />Success<br />200 OK<br />Failure<br />200 OK with explanation<br />
23. Global<br />User is not allowed to access resource<br />403 Forbidden<br />User is not logged in<br />401 Unauthorized<br />