Using this tutorial Drupal Console's drupal gprr
command, I've created a custom REST resource in Drupal 8. I'm able to use this resource to retrieve a simple JSON object that says hello to the current user by doing a GET on:
http://localhost/example/hello?_format=json
The JSON returned then looks like this:
{ "hello": "tyler" }
This is to be expected, as I'm logged in. But when I logout, and do another GET on the resource, the same result is returned:
{ "hello": "tyler" }
Doing a full Drupal cache clear fixes this issue, but I'd like to specifically disable caching on this resource. How can I do that?
As a workaround, I thought adding a time stamp to the query string would work:
http://localhost/example/hello?_format=json&time=123456789
But that also returns the exact same result, no matter what time stamp value is in the query string.
For brevity, here's the skeleton code for building the GET resource:
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()
));
}
}
Is there a configuration/setting I need to set on this plugin to disable caching on GET requests?