You are on page 1of 14

Table

of Contents
Introduction 1.1
Install SLIM Framework 1.2
CRUD By SLIM 1.3
Preparation 1.4
Method GET All 1.5
Method GET By ID 1.6
Method POST 1.7
Method PUT 1.8
Method DELETE 1.9
Testing 1.10
Source Code 1.11

1
Introduction

What is REST API?


REST (REpresentational State Transfer) is one of architecture communication
method which often in development web services.

REST of architecture, commonly run via HTTP (Hypertext Transfer Protocol),


involves the process of reading specific web page that loads a file XML or JSON.
This files are describes and that loads content to be presented. After going
through a process a special definition, customers can access the intended
application interface.

REST is often used in mobile application, social media, mashup tools, and
automated business processes.

What is SLIM Framework?


PHP micro framework that helps PHP developers quickly and easily write web
applications and APIs. Think of it as a core set of tools with which a developer
can build amazing things."

Called micro framework bacause Slim framework which focus on the basic needs
required of a web application such as: received a HTTP request, send request to
appropriate code, and restore HTTP responses.

2
Install SLIM Framework

How to install SLIM Framework?


Before readers install SLIM framework it would be nice readers adjust a PHP
version used. There are two requirements which readers must fullfill. They are:

Web Srver with URL Rewriting


PHP Version 5.5 or latest

For install SLIM readers can use composer

composer require slim/slim "^3.0"

3
CRUD By SLIM

What is CRUD?
CRUD is an abbreviation of Create , Read , Update and Delete . On a
complex application will definitely use this term.

Create
Create means we will create function Add . Where that function is function for
add new data into our database. If in REST means we will create method POST

Read
Read is a function for display data from our database that we have into View
template. It means show in the form of Loop or depends of Parameters . In
the REST we use method GET

Update
Update or Edit is function for update data that we have. In the REST we use
method PUT

Delete
Function of Delete used for deleting of data. In the REST we can use method
DELETE

4
Preparation

Before Do Coding
Before we do coding there are some that should be needed. Such as:

MySQL
In this case i created a database as name db_movie _ _and consist of one table
t_movie . To make that table so you can type like the code below

CREATE TABLE t_movie (id_movie int auto_increment primary_key no


t null, movie_title varchar(30) not null, movie_rate int not nul
l )

.htaccess
Create one file named .htaccess. This file requires for clean url. Type like the
code below

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Config
Create one file named config.php in main directory and then type like the code
below

5
Preparation

function getDB() {
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="db_movie";
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname",
$dbuser, $dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_
EXCEPTION);
return $dbConnection;
}

index
Create one file too named index.php in main directory and then type like the
code below. This code is needed to required file config and autoload which
exist in SLIM directory

require 'vendor/autoload.php';
include 'config.php';
$app = new Slim\App();
//Your Main Code
$app->run();

6
Method GET All

Method GET All


This method used for display all data in your table in the form of JSON format

$app->get('/movies', function ($request,$response) {


try{
$con = getDB();
$sql = "SELECT * FROM t_movie";
foreach ($con->query($sql) as $data) {
$result[] = array(
'id_movie' => $data['id_movie'],
'movie_title' => $data['movie_title'],
'movie_rate' => $data['movie_rate']
);
}
if($result){
return $response->withJson(array('status' => 'true',
'result'=>$result),200);
}else{
return $response->withJson(array('status' => 'Movies
Not Found'),422);
}

}
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMess
age()),422);
}

});

7
Method GET By ID

Method GET By ID
This method used for display data which selected by ID in your table in the form of
JSON format

$app->get('/getmovie/{id}', function ($request, $response){


try{
$id = $request->getAttribute('id');
$con = getDB();
$sql = "SELECT * FROM t_movie WHERE id_movie = :id";
$pre = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO
::CURSOR_FWDONLY));
$values = array(
':id' => $id
);
$pre->execute($values);
$result = $pre->fetch();

if($result){
return $response->withJson(array('status' => 'true',
'result'=>$result),200);
}else{
return $response->withJson(array('status' => 'Movies
Not Found'),422);
}

}
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMess
age()),422);
}
});

8
Method POST

Method POST
In this method is you can insert data into your table

$app->post('/addmovie', function($request, $response){


try{
$con = getDB();
$sql = "INSERT INTO t_movie (movie_title, movie_rate) VAL
UES (:movie_title, :movie_rate)";
$pre = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO:
:CURSOR_FWDONLY));
$values = array(
':movie_title' => $request->getParam('movie_title')
,
':movie_rate' => $request->getParam('movie_rate')
);
$result = $pre->execute($values);
if($result){
return $response->withJson(array('status' => 'Movie A
dded'), 200);
}else{
return $response->withJson(array('status' => 'Movies
Not Added'),422);
}

}
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMessa
ge()),422);
}
});

9
Method PUT

Method PUT
PUT is on of for update your data in table

$app->put('/updatemovie/{id}', function($request, $response){


try{
$id = $request->getAttribute('id');
$con = getDB();
$sql = "UPDATE t_movie SET movie_title = :movie_title
, movie_rate = :movie_rate WHERE id_movie = :id_movie";
$pre = $con->prepare($sql, array(PDO::ATTR_CURSOR =>
PDO::CURSOR_FWDONLY));
$values = array(
':movie_title' => $request->getParam('movie_title')
,
':movie_rate' => $request->getParam('movie_rate'),
':id_movie' => $id
);
$result = $pre->execute($values);

if($result){
return $response->withJson(array('status' => 'Movie
Updated'), 200);
}else{
return $response->withJson(array('status' => 'Movies
Not Updated'),422);
}
}
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMess
age()),422);
}
});

10
Method DELETE

Method DELETE
This method used for delete or remove your data in the table

$app->delete('/deletemovie/{id}', function($request, $response){


$id = $request->getAttribute('id');
$con = getDB();
$sql = "DELETE FROM t_movie WHERE id_movie = :id_movie";
$pre = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO:
:CURSOR_FWDONLY));
$values = array(
':id_movie' => $id
);
$result = $pre->execute($values);

if($result){
return $response->withJson(array('status' => 'Movie Dele
ted'), 200);
}else{
return $response->withJson(array('status' => 'Movies Not
Deleted'),422);
}
});
$app->run();

11
Testing

Testing
For testing you can third party for run your API. You can use Postman application
already provided by Google Crome. You can download it.

For testing you can type this url

GET All
http://servername/yourdirectory/movies

GET By ID
http://servername/yourdirectory/getmovie/{id_movie}

POST
http://servername/yourdirectory/addmovie

PUT
http://servername/yourdirectory/updatemovie/{id_movie}

DELETE

12
Testing

http://servername/yourdirectory/deletemovie/{id_movie}

13
Source Code

Source Code
You can donwload in HERE

Thanks :)

14

You might also like