Skip to content

Instantly share code, notes, and snippets.

@woutersf
Last active January 4, 2017 08:52
Show Gist options
  • Select an option

  • Save woutersf/2bcbcdf2552cb34d5e754e566a6ce000 to your computer and use it in GitHub Desktop.

Select an option

Save woutersf/2bcbcdf2552cb34d5e754e566a6ce000 to your computer and use it in GitHub Desktop.
D8 output XML/Json
<?php
///////////////////JSON
//How to render Json from Drupal 8
$response = new Response();
$response->setContent(json_encode($mydata));
$response->headers->set('Content-Type', 'application/json');
return $response;
//How to render Json from drupal 8
use Symfony\Component\HttpFoundation\JsonResponse;
return new JsonResponse($mydata);
///////////////////XML
// How to render XML from drupal 8
// This example is used in a block.
$output = '<xml></xml>';
$filename = 'output.xml';
$response = new CacheableResponse($output, Response::HTTP_OK, [
'content-type' => 'application/xml',
'Content-Disposition' => 'attachment;filename=' . $name,
]);
$meta_data = $response->getCacheableMetadata();
$meta_data->addCacheTags(['epg_xml', 'yout_custom_cacge_tags']);
return $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment