Created
July 23, 2015 21:52
-
-
Save signalpoint/97bfd628f47a5ddaeb05 to your computer and use it in GitHub Desktop.
Drupal 8 Output JSON
This file contains 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 | |
// ... | |
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
For anyone arriving here from Google: JsonResponse works well these days. Might not have back in 2016.