I would like to know the best coding practice that allows the client to define the response format for the request he made, which can also include filters, conditions, ordering, etc.
I made a small template for the answer, I do not know if it's the best practice, but it works (advice is welcome). I coded in the middleware after ();
PS: The format choice will be dynamic after building the request code.
Now about the requisition. I imagined coding in Middleware before (). What is the best way to do it?
Follow the code:
index.php
<?php
define('ROOT', dirname(__DIR__));
chdir(ROOT);
require 'vendor/autoload.php';
require 'src/Config/bootstrap.php';
require 'src/Config/routes.php';
$app->run();
bootstrap.php
<?php
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$app = new Application();
$app['serializer'] = function(){
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
return new Serializer($normalizers, $encoders);
};
$app['debug'] = true;
/*$app->before(function (Request $request) use ($app){
$request->query->
});*/
$app->after(function (Request $request, Response $response) use ($app){
//var_dump($response);
$response->headers->set('Content-Type', 'application/xml');
return $response;
});
return $app;
routes.php
<?php
$app->mount('/classificados', require 'src/App/Controllers/ClassificadosController.php');
ClassifiedsController.php
<?php
use Symfony\Component\HttpFoundation\Response;
$classificados = $app['controllers_factory'];
$classificados->get('/', function() use ($app) {
$post = array(
'title' => 'Titulo',
'body' => 'corpo',
);
$serializeContent = $app['serializer']->serialize($post, 'xml');
return new Response($serializeContent, 200);
});
return $classificados;
What is the best way to build a logic to dynamize the response format (json or xml) for the client?
UPDATE
I refactored my code by @Guilherme Nascimento's response, I had the idea of always returning a json from Controller and after () if an xml was requested to deserialize the return and serialize to a new response in xml format, if it is requested a json would return the response itself without performing this procedure, with the intention of abstracting this procedure from each route? Is it too costly for the server?
It looks like this:
bootstrap.php
<?php
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$app = new Application();
$app['debug'] = true;
$app['formatSerialize'] = function($format){
$app['formatSerialize'] = $format;
};
$app->before(function (Request $request) use ($app) {
$app['formatSerialize'] = (array_key_exists('xml', $request->query->all()) == 1) ? 'xml' : 'json';
});
$app->after(function (Request $request, Response $response) use ($app){
if ($app['formatSerialize'] == 'xml'){
$serializer = new Serializer(array(new ObjectNormalizer()), array(new XmlEncoder(), new JsonEncoder()));
$data = json_decode($response->getContent(), true);
$serializeContent = $serializer->serialize($data, $app['formatSerialize']);
$resp = new Response($serializeContent, $response->getStatusCode());
$resp->headers->set('Content-Type', 'application/xml');
return $resp;
}
$response->headers->set('Content-Type', 'application/json');
return $response;
});
return $app;
ClassifiedsController.php
<?php
$classificados = $app['controllers_factory'];
$classificados->get('/', function() use ($app) {
$post = array(
'title' => 'Titulo',
'body' => 'corpo',
);
return $app->json($post, 200);
});
return $classificados;
Is this deserialization and re-serialization very costly?