Last active
August 29, 2015 17:04
-
-
Save eminetto/5477c1da39f36d061fee to your computer and use it in GitHub Desktop.
This file contains 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 Aura\Router\RouterFactory; | |
use Zend\Expressive\AppFactory; | |
use Zend\Expressive\Router\Aura as AuraBridge; | |
use Zend\Diactoros\Response\JsonResponse; | |
require 'vendor/autoload.php'; | |
$auraRouter = (new RouterFactory())->newInstance(); | |
$router = new AuraBridge($auraRouter); | |
$app = AppFactory::create(null, $router); | |
$beers = array( | |
'brands' => array('Heineken', 'Guinness', 'Skol', 'Colorado'), | |
'styles' => array('Pilsen' , 'Stout') | |
); | |
$app->get('/', function ($request, $response, $next) { | |
$response->getBody()->write('Hello, beers of world!'); | |
return $response; | |
}); | |
$app->get('/brand', function ($request, $response, $next) use ($beers) { | |
return new JsonResponse($beers['brands']); | |
}); | |
$app->get('/style', function ($request, $response, $next) use ($beers) { | |
return new JsonResponse($beers['styles']); | |
}); | |
$app->get('/beer{/id}', function ($request, $response, $next) use ($beers) { | |
$id = $request->getAttribute('id'); | |
if ($id == null) { | |
return new JsonResponse($beers['brands']); | |
} | |
$key = array_search($id, $beers['brands']); | |
if ($key === false) { | |
return new JsonResponse('Not found', 404); | |
} | |
return new JsonResponse($beers['brands'][$key]); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment