Skip to content

Instantly share code, notes, and snippets.

@murindwaz
Forked from lrebrown/gist:4510874
Created July 7, 2013 03:01
Show Gist options
  • Save murindwaz/5942107 to your computer and use it in GitHub Desktop.
Save murindwaz/5942107 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
class test implements JsonSerializable
{
public $data = array();
public function jsonSerialize()
{
return get_object_vars($this);
}
}
$app['test'] = new test();
$app->before(function (Request $request) use($app) {
$app['test']->data['is-json'] = false;//default
$app['test']->data['getcontent-type-before-replace'] = gettype($request->getContent());
$app['test']->data['getcontent-before-replace'] = $request->getContent();
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$app['test']->data['is-json'] = true;
$data = json_decode($request->getContent(), true);
if (null === $data) {
return new Response('{"message":"Problems parsing JSON"}', 400);
}
$app['test']->data['conversion-success'] = true;
$app['test']->data['data-type-after-conv'] = gettype($data);
$app['test']->data['data-after-conv'] = $data;
$app['test']->data['request-type-before-replace'] = gettype($request->request);
$app['test']->data['request-before-replace'] = $request->request;//might be appearing empty due to lack of JsonSerializable on class??
//$request->request->replace(is_array($data) ? $data : array());
$request->request->replace($data);
$app['test']->data['request-type-after-replace'] = gettype($request->request);
$app['test']->data['request-after-replace'] = $request->request;//might be appearing empty due to lack of JsonSerializable on class??
}
$app['test']->data['getcontent-type-after-replace'] = gettype($request->getContent());
$app['test']->data['getcontent-after-replace'] = $request->getContent();
});
$app->post('/admin/usergroups', function() use($app) {
$app['test']->data['getcontent-type-in-controller'] = gettype($app['request']->getContent());
$app['test']->data['getcontent-in-controller'] = $app['request']->getContent();
return new Response(json_encode($app['test']->data), 200, array('Content-Type' => 'application/json'));
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment