RESTful API Slim Framework

Selamat Pagi kawan-kawan, kali ini mau share nih tentang RESTful API menggunakan Slim Framework, kenapa pakai slim framework? sebenernya mau coba-coba aja, oiya disini yang dibahas metode request umum, seperti GET, POST, DELETE dan PUT, yuk ikuti langkah-langkah nya.

Langkah Pertama
Instal slim framework.

Langkah Kedua
Tambahkan Eloquent pada aplikasi dengan composer

composer require illuminate/database "~5.1"

Langkah Ketiga
Siapkan tabel Mahasiswa.

create sruktur tabel sebagai berikut:

CREATE TABLE `mahasiswa` (
`nim` varchar(10) NOT NULL,
`nama_lengkap` varchar(50) NOT NULL,
`tanggal_lahir` date NOT NULL,
`no_hp` varchar(13) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Langkah Keempat
Koneksi kedatabase

'db' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'database',
            'username' => 'user',
            'password' => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ]

Langkah Kelima
Jangan lupa pada file dependencis

// Service factory for the ORM
$container['db'] = function ($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container['settings']['db']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

Langkah Keenam
Buat metode request RESTful API

1. Method GET (menampilkan semua data)

//Method GET
$app->get("/allmahasiswa/", function (Request $request, Response $response){
$sql = "SELECT * FROM mahasiswa";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
return $response->withJson(["status" => "success", "data" => $result], 200);
});

cek pada RESTClient

Preview

2. Method POST (menambah data)

//Method POST
$app->post("/mahasiswa/", function (Request $request, Response $response){ 
$emp = json_decode($request->getBody()); 
$sql = "INSERT INTO mahasiswa (nim, nama_lengkap) VALUE (:nim, :nama_lengkap)";
$stmt = $this->db->prepare($sql);
$data = [
":nim" => $emp->nim,
":nama_lengkap" => $emp->nama_lengkap 
];
if($stmt->execute($data))
return $response->withJson(["status" => "success", "data" => "1"], 200);

return $response->withJson(["status" => "failed", "data" => "0"], 200);
});

Cek pada RESTClient

Preview

3. Method PUT dan DELETE

//Method PUT
$app->put("/mahasiswa/{id}", function (Request $request, Response $response, $args){
$id = $args["id"];
$emp = json_decode($request->getBody()); 
$sql = "UPDATE mahasiswa SET nama_lengkap=:nama_lengkap WHERE nim=:id";
$stmt = $this->db->prepare($sql);
$data = [
":id" => $id,
":nama_lengkap" => $emp->nama_lengkap
];

if($stmt->execute($data))
return $response->withJson(["status" => "success", "data" => "1"], 200);

return $response->withJson(["status" => "failed", "data" => "0"], 200);
});

//Method DELETE
$app->delete("/mahasiswa/{id}", function (Request $request, Response $response, $args){
$id = $args["id"];
$sql = "DELETE FROM mahasiswa WHERE nim=:id";
$stmt = $this->db->prepare($sql);
$data = [
":id" => $id
];
if($stmt->execute($data))
return $response->withJson(["status" => "success", "data" => "1"], 200);

return $response->withJson(["status" => "failed", "data" => "0"], 200);
});

Cek pada RESTClient dan Preview (PUT & Delete)

Done.

Salam
Okto

References:
https://www.petanikode.com/slim-api/
https://www.phpflow.com/php/create-simple-rest-api-using-slim-framework/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s