Skip to header Skip to main navigation Skip to main content Skip to footer
  • Log in
Home
DrupalVIP
Freelancer service with great care and enthusiasm.
  • Home
  • Products
  • Blog
  • Support
    • Dictionary
    • Drupal Resources
    • External Resources

ResourceResponse When and How

Breadcrumb

  • Home
  • DrupalVIP Support
  • ResourceResponse When and How

Creating the Resource Plugins

In order to create a custom REST service, you will need to become acquainted with Plugins. 
Plugins are small pieces of functionality that are swappable. 
Plugins that perform similar functionality are of the same plugin type. 
In the case of our custom resource, we need to extend the ResourceBase plugin, which can be found here

In order to implement a ResourceBase plugin in our REST module, create a new fileDemoResource.php in /src/Plugin/rest/resource/ directory of the Demo REST API module and add the following:

namespace Drupal\demo_rest_api\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

class ExampleHello extends ResourceBase {

  /**
   * {@inheritdoc}
   */
  public static function create(/* ... */) {
    return new static(/* ... */);
  }

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   */
  public function __construct(/* ... */) {
    parent::__construct(/* ... */);
    // ...
  }

  /*
   * Responds to GET requests.
   */
  public function get() {

    $account = \Drupal::currentUser()->getAccount();
    if (!$account->id()) {
      return new ResourceResponse(array(
        'welcome' => 'visitor'
      ));
    }
    return new ResourceResponse(array(
      'hello' => $account->getAccountName()
    ));
  }

}

class ModifiedResourceResponse

A response that does not contain cacheability metadata.
Used when resources are modified by a request: responses to unsafe requests (POST/PATCH/DELETE) can never be cached.

In order to user library Drupal\rest, you must activate the module : RESTful Web Services

 

Drupal Development

Drupal Development
Code Snippet
$response = ['message' => 'Hello, this is a rest service'];
return new ResourceResponse($response);
use Symfony\Component\HttpFoundation\Response;

$response = new Response(
    'Content',
    Response::HTTP_OK,
    ['content-type' => 'text/html']
);

$response->setContent('Hello World');

// the headers public attribute is a ResponseHeaderBag
$response->headers->set('Content-Type', 'text/plain');

$response->setStatusCode(Response::HTTP_NOT_FOUND);
Read More
class ResourceResponse
Custom REST Resources
class ModifiedResourceResponse
Response
Response.php
Tags
Drupal Support
Drupal 10
ResourceResponse
response
Drupal Response
Resource Plugin
Drupal Plugin
REST Service
ResourceBase
ModifiedResourceResponse
RESTful Web Services
HttpFoundation Component
Symfony Response Example

The Freelancer Assistance

Drupal Training

drupal training session

Site Management

Site Management

Drupal Development

Drupal Development

Proactive Maintenance

Proactive Maintenance

Drupal Shared Space

Drupal Shared Space

Fullstack Service

Fullstack Service
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Footer menu

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP project of Automatic Frameworks - All rights reserved

Developed and Maintain by Jonathan Ben Hur Freelancer