PHP code example of palanik / restslim

1. Go to this page and download the library: Download palanik/restslim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

palanik / restslim example snippets


$app = new \Slim\Slim();

// This should obviously go in a datastore
$data = array();
$data["1"] = array("id" => 1,
                "message" => "Hello, World!");
$data["2"] = array("id" => 2,
                "message" => "Good Bye!");


$greetings = new \RestSlim\RestSlim("greetings");

// List
$greetings->list(function() use ($app, $data) {
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->write(json_encode($data, JSON_NUMERIC_CHECK));
});

// Read
$greetings->get(function($id) use ($app, $data) {
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->write(json_encode($data[$id], JSON_NUMERIC_CHECK));
});

// Create
$greetings->create(function() use ($app, $data) {
	$request = $app->request();
	$message = json_decode($request->getBody(), true);
	$id = count($data) + 1;
	$message["id"] = $id;
	$data[$id] = $message;
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->write(json_encode($data, JSON_NUMERIC_CHECK));
})
// Update
$greetings->update(function($id) use ($app, $data) {
	$request = $app->request();
	$message = json_decode($request->getBody(), true);
	$message["id"] = $id;
	$data[$id] = $message;
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->write(json_encode($data, JSON_NUMERIC_CHECK));
})
// Delete
->delete(function($id) use ($app, $data) {
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->write(json_encode($data[$id], JSON_NUMERIC_CHECK));
    unset($data[$id]);
});

// Inject into Slim
$greetings->app($app)
            ->run();