slim
Beberapa waktu lalu seorang teman (Edwin) membuatkan webservice (SOAP) dengan menggunakan java (http://210.247.242.106/GISService/Service?wsdl) tentang database provinsi, kecamatan dan desa di Indonesia yang didapatkan dari Panji09 pada thread di kaskus.
Kali ini saya mencoba untuk membuat webservice (REST) sederhana dengan menggunakan Slim Framework.

Apa itu REST?

REST adalah Representational State Transfer.
– REST menggunakan protokol HTTP (Create, Read, Update, Delete) dengan metode POST, GET, PUT, DELETE.
– REST alternatif dari RPC atau webservice SOAP, WSDL dll.
– REST sebenarnya bukanlah sebuah standar, hanya sebuah Architectural style bukan seperti SOAP menggunakan protokol.

Kenapa Slim Framework?

– Karena mirip dengan Sinatra (Ruby).
– Slim menggunakan PHP >= 5.3.0.
– Ukurannya relatif kecil.
– Mudah dipelajari.

Fitur

Powerful router
Standard and custom HTTP methods
Route parameters with wildcards and conditions
Route redirect, halt, and pass
Route middleware
Template rendering with custom views
Flash messages
Secure cookies with AES-256 encryption
HTTP caching
Logging with custom log writers
Error handling and debugging
Middleware and hook architecture
Simple configuration

Contoh penggunaan slim dengan koneksi ke database lewat PDO :

<?php
require 'Slim/Slim.php';
require 'db.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'mode' => 'production',
	'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
        'path' => './logs',
        'name_format' => 'Y-m-d',
        'message_format' => '%label% - %date% - %message%'
		))
));
    
$app->contentType("application/json");

$app->get('/getProvinsis', 'getProvinsis');
$app->get('/getKabupatens/:id', 'getKabupatens');
$app->get('/getKecamatans/:id', 'getKecamatans');
$app->get('/getDesas/:id', 'getDesas');

// Only invoked if mode is "production"
$app->configureMode('production', function () use ($app) {
    $app->config(array(
        'log.enable' => true,
		'log.level' => \Slim\Log::WARN,
        'debug' => false
    ));
});

// Only invoked if mode is "development"
$app->configureMode('development', function () use ($app) {
    $app->config(array(
        'log.enable' => false,
		'log.level' => \Slim\Log::DEBUG,
        'debug' => true
    ));
});

$app->notFound(function () use ($app) {
   echo 'notFound coy';
});

$app->run();

//All Functions goes here
function getProvinsis() 
{
    global $app;

    $sql = "select * FROM provinsi ORDER BY id";
    try {
        $app->etag('getProvinsis');
        $db = getConnection();
        $stmt = $db->query($sql);
        $provinsis = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($provinsis);
    } catch(\PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

function getKabupatens($id) 
{
    $sql = "SELECT * FROM kabupaten WHERE id_prov = :id";
    
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $kabupatens = $stmt->fetchAll();
        $db = null;
        echo json_encode($kabupatens);
    } catch (\PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

function getKecamatans($id) 
{
    $sql = "SELECT * FROM kecamatan WHERE id_kabupaten = :id";
    
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $kecamatans = $stmt->fetchAll();
        $db = null;
        echo json_encode($kecamatans);
    } catch (\PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

function getDesas($id) 
{
    $sql = "SELECT * FROM desa WHERE id_kecamatan = :id";

    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $desas = $stmt->fetchAll();
        $db = null;
        echo json_encode($desas);
    } catch (\PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

Download

Anda dapat mendownload source code di https://github.com/jefrip/GISService

Referensi :
https://github.com/edwinkun/GISService
http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/

Print Friendly, PDF & Email