Пример #1
0
<?php

require_once '/include/dbHandler.php';
require_once '/lib/Slim/Slim/Slim.php';
Slim\Slim::registerAutoloader();
$app = new Slim\Slim();
//Get All friends end point
$app->get('/friends', function () {
    $db = new dbHandler();
    $cur = $db->getAllFriends();
    //Variable to store result
    $result = array();
    //Do itteration for all document in a collection
    foreach ($cur as $doc) {
        $tmp = array();
        //Set key and get value from document and store to temporary array
        $tmp["name"] = $doc["name"];
        $tmp["age"] = $doc["age"];
        //push temporary array to $result
        array_push($result, $tmp);
    }
    //show result
    response(200, $result);
});
//Post Friends end point
$app->post('/friends', function () use($app) {
    $res = array();
    $name = $app->request()->post('name');
    $age = $app->request()->post('age');
    $db = new dbHandler();
    $cur = $db->insertFriend($name, $age);