Skip to content

Instantly share code, notes, and snippets.

@localdisk
Created January 27, 2014 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localdisk/8643113 to your computer and use it in GitHub Desktop.
Save localdisk/8643113 to your computer and use it in GitHub Desktop.
Response::xml macro
<?php
Response::macro('xml', function(array $vars, $status = 200, array $header = [], $xml = null)
{
if (is_null($xml)) {
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>');
}
foreach ($vars as $key => $value) {
if (is_array($value)) {
Response::xml($value, $status, $header, $xml->addChild($key));
} else {
$xml->addChild($key, $value);
}
}
if (empty($header)) {
$header['Content-Type'] = 'application/xml';
}
return Response::make($xml->asXML(), $status, $header);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment