Drupal Up - Pal up with drupal

Custom JSON response Drupal 8 module


In this video, we are going to be programming a custom module with a controller that returns JSON response.

As always the full code of the example could be found on Github here, where if you have any doubts about your implementation you could directly download and try the tested version and also faster compare and debug your code.

Creating a simple Drupal 8 module with info file and controller we've done many times in the Drupal Up YouTube channel. The interesting part here is the controller that is returning the JSON response. It looks like this:

<?php

namespace Drupal\drupalup_json_api\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * Implementing our example JSON api.
 */
class JsonApiController {

  /**
   * Callback for the API.
   */
  public function renderApi() {

    return new JsonResponse(
      'data' => $this->getResults(),
      'method' => 'GET',
    );
  }

  /**
   * A helper function returning results.
   */
  public function getResults() {
    return [
      [
        "name" => "The Shawshank Redemption",
        "year" => 1994,
        "duration" => 142,
      ],
      [
        "name" => "The Godfather",
        "year" => 1972,
        "duration" => '',
      ],
      [
        "name" => "The Dark Knight",
        "year" => 2008,
        "duration" => 175,
      ],
      [
        "name" => "The Godfather: Part II",
        "year" => 1974,
        "duration" => '',
      ],
      [
        "name" => "Pulp Fiction",
        "year" => 1994,
        "duration" => '',
      ],
      [
        "name" => "The Lord of the Rings: The Return of the King",
        "year" => 2003,
        "duration" => '',
      ],
    ];
  }

}

As you see it is quite simple. We just use the Symfony's JsonResponse component and we return it in our controller callback with the result array that we build in the getResults method. 

For this use case the .routing.yml file also looks a bit different:

drupalup_json_api.json_api:
  path: '/api/json'
  defaults:
    _controller: 'Drupal\drupalup_json_api\Controller\JsonApiController::renderApi'
    _title: 'JSON api'
  methods:  [GET]
  requirements:
    _access: 'TRUE'

It is a good idea to define also methods and _access - if you want it to be accessible for all people that try to access the page - we could just set to TRUE.

Watch, read, try and if you have any questions - don't hesitate to ask them here or in the YouTube video. 

If you like the YouTube channel and you want to see more videos - please subscribe to it from this link here.

Thanks for reading, watching, sharing.

Meet the author

Nikolay is a Drupal PHP developer with more than 7 years of experience. Passion for teaching, passion for simplicity and effectiveness of the code are just some keywords that he cares about. See more ...
Drupal Up is a platform for learning Drupal ...

The idea is though not just to be another platform with some boring videos but all the videos to be inspired by real-world problems. That means something that you will most probably need if you work with Drupal. We want you to be successful and we want to teach you everything we know about Drupal!
So watch the videos, try to follow and reproduce, ask questions and you will see you will be able to conquer this wonderful sea that Drupal 8 is.