-
Notifications
You must be signed in to change notification settings - Fork 173
Specifying available HTTP methods for each field
Your resource may need a public field that isn't actually a property or field. RESTful provides a solution for this, and in 2.x it's very flexible.
In RESTful 1.x, you could specify create_or_update_passthrough
in your public field definitions.
"create_or_update_passthrough": Determines if a public field that isn't mapped to any property or field, may be passed upon create or update of an entity. Defaults to FALSE.
In RESTful 2.x, instead of create_or_update_passthrough
you may specify the HTTP methods that should be available for each field. An example should clarify the concept:
$public_fields['barry'] = array(
// your field definition here...
'methods' => array(RequestInterface::METHOD_GET, RequestInterface::METHOD_OPTIONS),
);
RequestInterface::METHOD_GET
is a constant defined in RequestInterface
; you could instead simply use the string GET
. If you use the constant (and it's best practice to do so) then you will need to be sure to include the RequestInterface use
statement:
use Drupal\restful\Http\RequestInterface;
If you examine that PHP interface, you will find the following METHOD constants:
const METHOD_HEAD = 'HEAD';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_PATCH = 'PATCH';
const METHOD_DELETE = 'DELETE';
const METHOD_PURGE = 'PURGE';
const METHOD_OPTIONS = 'OPTIONS';
const METHOD_TRACE = 'TRACE';
const METHOD_CONNECT = 'CONNECT';