Example #1
0
<?php

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
$methodType = $_SERVER['REQUEST_METHOD'];
if ($methodType === 'GET') {
    if (isset($_GET["type"]) && !empty($_GET["type"])) {
        $type = $_GET["type"];
        $rows = getFavourites($type);
        if ($type = "JSON") {
            renderJSON($rows);
        } else {
            echo "WRONG TYPE";
        }
    } else {
        echo "NEED TYPE";
    }
} else {
    echo "NEED GET";
}
function renderJSON($rows)
{
    $data = array("status" => "success", "favs" => $rows);
    echo json_encode($data, JSON_FORCE_OBJECT);
}
//get favs
function getFavourites($type)
{
    $servername = "localhost";
    $dblogin = "******";
Example #2
0
    $id = $args['id'];
    $beardDB = new Beard();
    $beard = $beardDB->updateBeard($id);
    if ($beard) {
        return $response->withStatus(302)->withHeader('Location', '/slimapi/index.php/beards');
    } else {
        return $response->withStatus(500)->withHeader('Location', '/slimapi/index.php/beards');
    }
});
// Create action .json
$app->post('/beards.json', function ($request, $response) {
    $body = $request->getParsedBody();
    // $_POST associative array
    $beardDb = new Beard();
    $beard = $beardDb->createBeard($body);
    return renderJSON($response, 201, $beard);
});
// Create action
$app->post('/beards', function ($request, $response) {
    $body = $request->getParsedBody();
    // $_POST associative array
    $beardDb = new Beard();
    $beardDb->createBeard($body);
    return $response->withStatus(302)->withHeader('Location', '/slimapi/index.php/beards');
});
$app->delete('/beards/{id}', function ($request, $response, $args) {
    $id = $args['id'];
    $beardDB = new Beard();
    $beard = $beardDB->deleteBeard($id);
    if ($beard) {
        return $response->withStatus(302)->withHeader('Location', '/slimapi/index.php/beards');