README.md

PHP PDO REST SERVER

Part 1

Creating a PHP REST Server using SLIM REST API Framework

This will cover the implementation of a REST Back-end for a Webserver. The PHP
SLIM framework library is lightweight and fast. Using this framework eliminates
the need to specially write your own which could easily face security issues.
Although REST provides the required connections, the developer will need to
write specific code for the Routes and Middleware to ensure that the dedicated
URL’s are setup correctly.

Scope

Set up a PHP REST Server and use a Debugger to test the Web Server.

Setting Up

It is best to use a local XAMP
or WAMP setup, an open
source Apache Server. This will load
PHP,
MYSQL or MARIA
database server depending on your Installation. Maria DB is an on-going Open
Source alternative to MySQL DB and is designed by the same developers for
MySQL DB. I am not going to explain how to set your Apache server for a
quick Google on this subject will provide the required steps to set this up.

Composer is used to download SLIM 3 into your
project. The SLIM 3 documentation explains how to perform this process.

Bootstrap 3 is a Responsive mobile first framework
and is only used to View the database.

A live test REST Server example of the code is available so that calls can be
easily made from a REST DEBUGGER and ultimately from the various working DELPHI
CLIENT Applications that will be presented over the FIVE PART SERIES. This TEST
REST SERVER allows both HTTP and HTTPS Calls to be made to it.

File Structure

Folder dist:- holds the bootstrap 3 library for the view.php file.

Folder public :-contains the index.php to which all the url calls are made
to.

.htaccess is an apache file.

The View.php:- shows a table view of the database. A button will call the
Resetdb.php which in turn resets the Database. This is handy should all the
data be deleted in your LocalHost Server.

Folder src:- contains the config folder for the db.php which connects to the
MySql / Maria Database.

Folder routes:- has the pricing.php file which is file that will technically
process all the REST Requests and Responses.

Folder vender:- is the Slim library.

Database

As this is a five part series Covering a pricelist REST server. The
salesREST.sql file has a single

pricelist table …

`salesREST`

Table structure for table `pricelist`

CREATE TABLE `pricelist` (

`id` int(11) NOT NULL,

`sku` varchar(20) DEFAULT NULL,

`skusize` varchar(20) DEFAULT NULL,

`barcode` varchar(25) DEFAULT NULL,

`description` varchar(100) DEFAULT NULL,

`price` float(30,2) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Create the index.php

To activate SLIM framework, we add the four lines below.

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;

use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

Instead of writing all the code to the index.php, all the call Requests are sent
to the routes folder where the pricing.php file receives each REST Request,
processes the Request and provides a REST Response. For a larger REST Server,
More routes would be introduced.

//Products and Pricing SRC Routes

require '../src/routes/pricing.php';

$app->run();

?>

Adding the Database connection db.php

This uses the PDO connection procedure and is wrapped in a class database.

First the call to use the REST Request interface and the REST Response
interface of SLIM.

Then call the SLIM framework.

SLIM app told to use the folder routes and the function requirement is REST
Request returning a REST Response (back to the client).

A middleware function is created by calling ‘$app-> add’ to accept
authorization Headers (if required) and to accept the methods GET, POST, PUT
and DELETE and OPTIONS. This invokes a PSR 7 Response ‘withHeader’.

Call to SLIM to get the URL and the function to process the REST Request and
return a REST Response.

The pricelist table is accessed and all data is fetched as a PDO object then
encoded into JSON format, sent as the REST Request to the Client. If there
is an error then the Error REST Response is sent.