Last active
August 29, 2015 14:15
-
-
Save fhferreira/1bc6f4aaa1f4b64db865 to your computer and use it in GitHub Desktop.
Silex API easy
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
{ | |
"require": { | |
"silex/silex": "~1.2", | |
"doctrine/dbal": "2.2.*" | |
} | |
} |
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 | |
require_once __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\HttpKernel\Debug\ErrorHandler; | |
use Symfony\Component\HttpKernel\Debug\ExceptionHandler; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\Request; | |
// set the error handling | |
ini_set('display_errors', 1); | |
error_reporting(-1); | |
ErrorHandler::register(); | |
if ('cli' !== php_sapi_name()) { | |
ExceptionHandler::register(); | |
} | |
$app = new Silex\Application(); | |
$app->after(function (Request $request, Response $response) { | |
$response->headers->set('Access-Control-Allow-Origin', '*'); | |
}); | |
$app->register(new Silex\Provider\DoctrineServiceProvider(), | |
array( | |
'dbs.options' => | |
array ( | |
'mysql_read' => | |
array( | |
'driver' => 'pdo_mysql', | |
'host' => 'localhost', | |
'dbname' => 'nomes', | |
'user' => 'user', | |
'password' => 'password', | |
'charset' => 'utf8', | |
) | |
), | |
)); | |
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array( | |
'http_cache.cache_dir' => __DIR__.'/cache/', | |
)); | |
$app->get('/sugestao/{sexo}', function($sexo) use($app) { | |
//return 'sugestões: '.$app->escape($sexo); | |
$sql = "SELECT * FROM nomes WHERE sexo = ? order by rand() limit 0,10"; | |
$nomes = $app['dbs']['mysql_read']->fetchAll($sql, array((string) $sexo)); | |
//return $app->json($nomes); | |
return new Response( | |
json_encode($nomes), | |
200, | |
[ | |
'Content-Type' => 'application/json', | |
'Cache-Control' => 's-maxage=5' | |
] | |
); | |
}); | |
$app->get('/significado/{nome}', function($nome) use($app) { | |
//return 'sugestões: '.$app->escape($sexo); | |
$sql = "SELECT * FROM nomes WHERE name like ? order by rand() limit 0,10"; | |
$nomes = $app['dbs']['mysql_read']->fetchAll($sql, array((string) $nome . '%')); | |
//return $app->json($nomes); | |
return new Response( | |
json_encode($nomes), | |
200, | |
[ | |
'Content-Type' => 'application/json', | |
'Cache-Control' => 's-maxage=5' | |
] | |
); | |
}); | |
$app->get('/nomes', function() use($app) { | |
//return 'sugestões: '.$app->escape($sexo); | |
$nomes = $app['dbs']['mysql_read']->fetchAll('SELECT * FROM nomes order by rand() limit 0,10'); | |
//return $app->json($nomes); | |
return new Response( | |
json_encode($nomes), | |
200, | |
[ | |
'Content-Type' => 'application/json', | |
'Cache-Control' => 's-maxage=5' | |
] | |
); | |
}); | |
$app->error(function (\Exception $e, $code) use ($app) { | |
if ($app['debug']) { | |
return; | |
} | |
echo print_r($e,true); | |
// ... logic to handle the error and return a Response | |
}); | |
$app['debug'] = true; | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment