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
1 2 3 4 5 |
<?php
include('lib/limonade.php');
?>
|
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)
1 2 3 4 5 6 7 8 9 10 11 |
CREATE DATABASE `lemondoo` DEFAULT CHARSET utf8;
USE `lemondoo`;
CREATE TABLE `todo` (
`todo_id` int(11) not null auto_increment,
`todo_title` varchar(100),
`todo_text` text,
`completed` tinyint(4) default '0',
PRIMARY KEY (`todo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
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”