Skip to content

Instantly share code, notes, and snippets.

@anyt
Created August 5, 2014 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anyt/e45a245576451c7abdf7 to your computer and use it in GitHub Desktop.
Save anyt/e45a245576451c7abdf7 to your computer and use it in GitHub Desktop.
<?php
use Express\Application;
use Symfony\Component\HttpFoundation\Request;
$app = new Application();
//REST API example
$app->match(
'/user',
function () use ($app) {
if ($app->request()->method() == "GET") {
$users = $app->db()->findAll('user');
return $app->json($users);
} else { // POST
$user = $app->request()->content();
$app->db()->insert('user', $user);
return $app->json($user);
}
}
)->method('GET|POST');
$app->match(
'/user/{id}',
function ($id) use ($app) {
$user = $app->db()->find('user', $id);
if (!$user) {
$app->abort(404, "No user found with id {$id}");
}
switch ($app->request()->method()) {
case 'GET':
return $app->json($user);
break;
case 'POST':
case 'PUT':
$user = $app->request()->content();
$app->db()->update('user', $id, $user);
return $app->json($user);
break;
case 'DELETE':
$app->db()->delete('user', $id);
return $app->response('success');
break;
}
return $app->json($user);
}
);
return $app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment