Last active
January 4, 2017 08:52
-
-
Save woutersf/2bcbcdf2552cb34d5e754e566a6ce000 to your computer and use it in GitHub Desktop.
D8 output XML/Json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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