Creating a Basic API in Drupal: Step-by-Step Guide Without Token Authentication

Follow this step-by-step guide to create a simple API in Drupal without using token authentication and by injecting custom services into your controller.

1. Define a Custom Service

First, define a custom service in your module's custom_api.services.yml file:

services:
  custom_api.example_service:
    class: Drupal\custom_api\ExampleService

2. Create the Service Class

Create the ExampleService class in the src directory of your module:

<?php

namespace Drupal\custom_api;

class ExampleService {

  public function getData() {
    return [
      'message' => 'Hello, this is a custom service response.',
      'status' => 'success',
    ];
  }

}

3. Update the Controller to Use the Service

Modify CustomApiController.php to inject the custom service and use it in your API response:

<?php

namespace Drupal\custom_api\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\custom_api\ExampleService;

class CustomApiController extends ControllerBase {
  protected $exampleService;

  public function __construct(ExampleService $exampleService) {
    $this->exampleService = $exampleService;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('custom_api.example_service')
    );
  }

  public function content(Request $request) {
    $data = $this->exampleService->getData();
    return new JsonResponse($data);
  }
}

4. Define the Route with Permissions

Ensure your route in custom_api.routing.yml is properly defined:

custom_api.content:
  path: '/api/custom'
  defaults:
    _controller: '\Drupal\custom_api\Controller\CustomApiController::content'
    _title: 'Custom API'
  requirements:
    _permission: 'access content'
  options:
    _format: 'json'

5. Enable the Module and Clear Cache

Enable your custom module and clear the cache:

drush en custom_api -y
drush cr

6. Test the API

Use a simple GET request to access your API endpoint:

curl -X GET http://your-drupal-site/api/custom

Conclusion

This setup demonstrates how to create a basic API in Drupal without bearer token authentication, focusing on the use of custom services and controllers. You can manage access control using Drupal’s built-in permissions.

Published By: Krishanu Jadiya
Updated at: 2024-08-06 00:17:19

Card Image

How to Set Up a Local SSL Certificate on Apache: Step-by-Step Guide

Learn how to set up a local SSL certificate on Apache with this comprehensive step-by-step guide. Secure your local development environment with HTTPS.

Card Image

Latest Features of Coding Technology

Explore the latest features and advancements in coding technology, including new programming languages, frameworks, DevOps tools, AI integration, and more.

Card Image

Understanding Laravel Mix Webpack Configuration: Step-by-Step Guide

Step-by-step explanation of a Laravel Mix Webpack configuration file, including asset management for JavaScript, CSS, and Vue.js support.

Card Image

How Emojis Can Enhance Your Git Commits | Gitmoji Guide

Discover how to enhance your Git commits with emojis. Learn about the best practices for creating informative and visually distinctive commit messages.