So I am trying to create a API for a social networking site, however SLIM by default does not have an easyway for a user to request xml response, or a ymal response.
I understand that this was mainly for PHP and JSON, but surely with other frameworks like api-platform supporting this multi-response making it easier for developers to code for all types of devices and uses.
Is their a plugin that allows a user to define the response that they would like?
Or do I need to make it a URL requirement example: api.site.com/me/photos.xml
and if so there should be a template system in slim that can see the .xml and load the .xml file from the template section.
Please if anyone has done this please leave a link to either documentation or better still tutoral.
Thank
I'm not sure what you mean by Slim being mainly for JSON as it has no bias whatsoever on the format of data rendered.
Personally, I use rka-content-type-renderer which renders an array into either JSON, XML or HTML based on the Accept Header.
This is a simple Slim app that uses it:
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false,
],
];
$app = new \Slim\App($config);
$app->get("/", function ($request, $response, $args) {
$data = [
'items' => [
[
'name' => 'Alex',
'is_admin' => true,
],
[
'name' => 'Robin',
'is_admin' => false,
],
],
];
$renderer = new RKA\ContentTypeRenderer\Renderer();
$response = $renderer->render($request, $response, $data);
return $response;
});
// Run!
$app->run();
Then, to output XML, the user simply requests XML via the HTTP Accept header:
$ curl -H "Accept: application/xml" http:/localhost:8888
and the output is:
<?xml version="1.0"?>
<root>
<items>
<name>Alex</name>
<is_admin>1</is_admin>
</items>
<items>
<name>Robin</name>
<is_admin>0</is_admin>
</items>
</root>
If you have any other questions regarding this issue, please reopen :)
Most helpful comment
I'm not sure what you mean by Slim being mainly for JSON as it has no bias whatsoever on the format of data rendered.
Personally, I use rka-content-type-renderer which renders an array into either JSON, XML or HTML based on the
AcceptHeader.This is a simple Slim app that uses it:
Then, to output XML, the user simply requests XML via the HTTP
Acceptheader:and the output is: