API Project - Part 1 - Set Up

Set Up and Configuration

Download the starter zip file and extract it. Then put the api folder in your doc root directory.

We are about to build a big complex system! We'll build it in parts and test each part as we go.

Most projects usually start with a datbase, here's the SQL to create our initial test database:

DROP DATABASE IF EXISTS api_test_db;

CREATE DATABASE api_test_db;

USE api_test_db;

CREATE TABLE user_roles (
  user_role_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  user_role_name varchar(30) NOT NULL,
  user_role_desc varchar(200) NOT NULL
);

INSERT INTO user_roles (user_role_id, user_role_name, user_role_desc) VALUES
(1, 'Standard User', 'Normal user with no special permissions'),
(2, 'Admin', 'Extra permissions');

CREATE TABLE users (
  user_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  user_first_name varchar(30) NOT NULL,
  user_last_name varchar(30) NOT NULL,
  user_email varchar(100) NOT NULL UNIQUE,
  user_password char(255) NOT NULL,
  user_salt char(32) NOT NULL,
  user_role INT NOT NULL DEFAULT '1',
  user_active boolean NOT NULL DEFAULT true,
  FOREIGN KEY (user_role) REFERENCES user_roles(user_role_id)
);

INSERT INTO users (user_first_name,user_last_name, user_email, user_password, user_salt, user_role, user_active) VALUES 
	('John', 'Doe','john@doe.com', 'opensesame', 'xxx', '1', true),
	('Jane', 'Anderson','jane@doe.com', 'letmein', 'xxx', '2', true),
	('Bob', 'Smith','bob@smith.com', 'test', 'xxx', '2', false);

Go ahead and run this script in PHPMyAdmin to create and populate your test database. Note that you don't have to create the database before running the script, because the script will create the database.

This framework consists of 4 main types of objects:

Before we dig into those files, we'll take a look at some other important files. Look at the code in index.php. The URL rewriting that has been set up in the .htaccess file will direct all requests to index.php. For now, this page simply echos the request method and the path has been requested (the $url_path variable). The path is relative to the api folder. What this means is that if you send a request to http:localhost/api/birds/eagles/22 then the $url_path variable will be set to /birds/eagles/22.

Try making a few requests to any old URL that begins with http:localhost/api/.

Now take a look at the config.inc.php file. There's a lot going on in this file, and some of it we won't worry about until later. But note the following:

  1. It includes files that set custom error handling and exception handling (we'll look at those in a minute).
  2. There is a rather large IF statement that checks the SERVER_NAME to see if it is localhost
    • If it IS localhost then we define the constants so they apply to our dev server (localhost)
    • If it is NOT localhost then we will have to define the constants so that they apply to our live server
    • Do not worry about all of the constants that are defined within this IF statement, we'll discuss many of them as we progress.
  3. The next IF statement will display all PHP errors to the screen if the DEBUG_MODE constant has been set to TRUE.
  4. There is a variable named $link which will serve as our connection to the database.
  5. There is a function name get_link() that will connect to the database and initialize the $link varialbe (and return it).
  6. Do not worry about all the code that comes after the get_link() function, we'll discuss it as we progress though the project.

Now take a look at custom_error_handler.inc.php and custom_exception_handler.inc.php and note that each file sets a custom handler (function) for error and exception handling. If you look inside the body of each handler, you'll see that if the DEBUG_MODE constant (declared in config.inc.php) is set to TRUE, then all error and exception info will be echoed into the HTTP response. If DEBUG_MODE is set to FALSE, then all error and exception info will be mailed the email address assigned to the SITE_ADMIN_EMAIL constant.