Skip to content

Instantly share code, notes, and snippets.

@willianmano
Created March 14, 2017 20:13
Show Gist options
  • Save willianmano/391f25cc079e86f35d7949cb71e81e50 to your computer and use it in GitHub Desktop.
Save willianmano/391f25cc079e86f35d7949cb71e81e50 to your computer and use it in GitHub Desktop.
API FOR DUMBIES
<?php
use Silex\Application;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
define("APP_ROOT", dirname(__DIR__));
chdir(APP_ROOT);
require "vendor/autoload.php";
$app = new Application();
$app['debug'] = true;
$app->get('/', function() {
return 'Hello world!';
});
$beers = [
['name' => 'Heineken', 'stars' => '5'],
['name' => 'Antarctica', 'stars' => '4'],
['name' => 'Cristal', 'stars' => '1']
];
$app->get('/beer', function() use ($beers) {
return new JsonResponse($beers, 200);
});
$app->get('/beer/{id}', function (Request $request, $id) use ($beers) {
foreach($beers as $beer) {
if(strtolower($id) == strtolower($beer['name'])) {
return new JsonResponse($beer, 200);
}
}
return new JsonResponse('Beer not found', 404);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment