Before I begin I just want to note that this document is a work in progress. It requires version 5.3+ of php as it utilizies anonymous functions and it requires the use of limonade-php v0.5.1
Get caught up
If you haven’t yet, check out Part 1 of this tutorial to get setup. I’m going to assume you did.
Step 1: Setup
First lets copy our “hello_world” directory and rename it to “lemondoo”. Then open your index.php file and leave delete everything except for the PHP open/close tags and the include statement.
Now, rename index.php to api.php
Step 2: Database!
Now we get to setup our database. Since each of us have different ways of doing this (I switch between command line and sqlbuddy) I’m providing the SQL code that will create our database. If you want you can re-create it using your favourite SQL manager.. or you can just copy and paste the SQL code and execute it. It’s nothing too complicated, just a single table called “todo” in a database named “lemondoo”. Each row in this table will have
- a “todo_id” which is an auto incremented primary key (int)
- a “todo_title” which is the title of this todo item (varchar(100))
- a “todo_text” which is the text of the todo item (text)
- a “completed” flag that is either 0 (not completed) or 1 (completed) (tinyint)
At this point, I would go ahead and enter a couple sets of data into our new table. Make sure that you set the completed field to 0.
Step 3: Design our API
API design, as far as I am concerned, should be an entire topic of study in itself. Adding REST principles makes it a little easier, but still it is something that should be thought about carefully. Below I’ve outlined the REST header, the associated URL and the function that it will call. Notice that we can have two different headers assigned to the same url and each can map to their own function call. What we’re going to do is define this route for limonade-php so that it knows what to do depending on what URL we try to access. Note below that when I say :id it means that if you access /anything it will call the appropriate method and also assign “anything” to the variable “id”. So if you had /:yes it would assign “anything” to the variable “yes”
|
HEADER |
URL |
Maps to |
|
GET |
/ |
get_todo_list | |
|
POST |
/ |
add_todo | |
|
GET |
:/id |
get_todo(id) | |
|
POST |
:/id |
update_todo(id) | |
|
DELETE |
:/id |
delete_todo(id) | |
Notice that limonade-php comes with some functions that mimic our Headers. dispatch_get, dispatch_post and dispatch_delete are all built in to limonade-php. Also notice that instead of passing an anonymous function to dispatch__xxx as we did previously, now we are passing a string. This string will be the name of the function that we will be creating. There are many different ways that you can provide “callbacks” to routes, and I would suggest that you read up on them in the readme
Normally, when I create an application the first thing I’ll include is a simple database abstraction class. For the purposes of this application, that is a little overkill, so here’s a function that will do everything we need it to.
I’m not going to explain it in too much detail, but essentially you pass in a SQL statement and it returns either an array-based resultset, or a true/false depending on if the statement succeeded.
Step 4: Configuring limonade
Step 5: Define your functions
get_todo_list():
Limonade-php abstracts away a lot of this work. Basically our db() method returns a two dimensional array containing all our todo’s and then that is converted into JSON using json_encode(). The json() provided by Limonade-php is simply a wrapper for that. Finally we return the JSON that we created. Limonade-php will hold the result until it runs through all it’s steps before printing out the JSON with the appropriate headers. If you save api.php and visit it from your browser, you should see a JSON representation of the todo’s in our
add_todo():
Notice that if the note was successfully inserted we are going to just grab the ID of the note and then return that.
get_todo():
This method will simply return the details of the todo requested. So accessing /1 will return all the details of the todo who’s todo_id is 1.
Notice that in the definition of our function we pass a single argument called $id. Remember how we defined dispatch_get('/:id','get_todo') earlier? Well now the value of :id is going to be passed to our function. We just do a quick check to make sure that the value passed to our function is valid and then we return the results of that dataset.
update_todo():
Update todo is going to accept all the fields in our todo table (todo_id, todo_title, todo_text, completed) and updated the database corresponding to todo_id. Then it returns either true or false depending on if the update worked. Since the end user will already have the updated data, there is no need to send anything else back.
delete_todo():
Finally, this method will delete any todo. We simply pass it an ID, and then BAM. Deleted.
Summary
So far, our API has been very simple. We can perform some basic actions on our database by accessing various URL’s. However, you will notice that you can not access “post”, “put” and “delete” pages through your browser natively. By default, browsers send a “GET” request, since you are trying to get the contents of a page. If you fill out a form, you get to do a “POST” request as well.
In our next section we will be building an HTML/JavaScript/CSS interface to work with our newly created API.