Created
August 5, 2014 09:14
-
-
Save anyt/e45a245576451c7abdf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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