Beispiel #1
0
    $name = $request['name'];
    $db = new Recipes();
    $item = $db->add_user_recipe($id, $name);
    if ($item) {
        // if successful, just return the JSON sent for use on the client-side
        $results = array('id' => $item, 'name' => $name);
        $app->response()->header('Content-Type', 'application/json');
        echo json_encode($results);
    }
});
// PUT route
$app->put('/recipes/:id', function ($id) use($app) {
    $request = (array) json_decode($app->request()->getBody());
    $name = $request['name'];
    $db = new Recipes();
    $items = $db->update_recipe($id, $name);
    if ($items) {
        // if successful, return the new object with the values
        $results = array('id' => $id, 'name' => $name);
        $app->response()->header('Content-Type', 'application/json');
        echo json_encode($results);
    } else {
        $app->response()->status(500);
    }
});
// DELETE route
$app->delete('/recipes/:id', function ($id) use($app) {
    $request = (array) json_decode($app->request()->getBody());
    $db = new Recipes();
    $items = $db->delete_recipe($id);
    if ($items) {