Professional Documents
Culture Documents
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
REST is often used in mobile application, social media, mashup tools, and
automated business processes.
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
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
.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
}
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
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
}
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
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
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.
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