Skip to content

Instantly share code, notes, and snippets.

@henrytran9x
Forked from signalpoint/customController.php
Created May 31, 2017 06:36
Show Gist options
  • Save henrytran9x/5e54bffcec015787e4315e5bf1812b6e to your computer and use it in GitHub Desktop.
Save henrytran9x/5e54bffcec015787e4315e5bf1812b6e to your computer and use it in GitHub Desktop.
Drupal 8 Output JSON
<?php
// ...
use Symfony\Component\HttpFoundation\Response;
// Then later on (inside your controller's class), you have a function used
// for the route_name (on a hook_menu() item in your .module file), this
// function can return a JSON response...
$response = new Response();
$response->setContent(json_encode(array('hello' => 'world', 'goodbye' => 'world')));
$response->headers->set('Content-Type', 'application/json');
return $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment