DocumentsControllerDeleteTrait uses \oihana\models\traits\ModelTrait, \oihana\controllers\traits\StatusTrait
Provides the `delete` method for a document-based controller.
This trait encapsulates the logic for handling HTTP DELETE requests to remove one or more documents. It is designed to be used in controllers that manage a resource collection, offering a flexible and robust deletion endpoint.
The implementation intelligently sources document IDs, prioritizing route
arguments (e.g., /collection/{id}) for single-document deletion and falling
back to a query parameter (e.g., /collection?id=1,2,3) for bulk operations.
Input IDs from the query string are automatically sanitized to remove duplicates
and empty values, and then sorted naturally.
It relies on ModelTrait to interact with the underlying data model and provides
lifecycle hooks (beforeDelete, afterDelete) for custom logic execution.
Standardized API responses are returned using StatusTrait.
Tags
Table of Contents
Methods
- delete() : ResponseInterface
- Deletes one or more documents from the collection.
Methods
delete()
Deletes one or more documents from the collection.
public
delete([ServerRequestInterface|null $request = null ][, ResponseInterface|null $response = null ][, array<string|int, mixed> $args = [] ][, array<string|int, mixed> $init = [] ]) : ResponseInterface
This method provides a flexible endpoint for document deletion. It can handle a single document ID provided as a route placeholder or one or more IDs supplied via a query parameter.
When multiple IDs are passed in the query string, they are cleaned to remove empty values and duplicates, then sorted using a natural sort algorithm before being processed by the model. The response format adapts to return the key of a single deleted document or an array of keys for multiple deletions.
Parameters
- $request : ServerRequestInterface|null = null
-
The PSR-7 request object, used to access query parameters.
- $response : ResponseInterface|null = null
-
The PSR-7 response object, used to build the HTTP response.
- $args : array<string|int, mixed> = []
-
An associative array of route placeholders. It is expected to contain the 'id' key for single-item deletion.
- $init : array<string|int, mixed> = []
-
An optional associative array to pass custom settings to the model layer (e.g., skipping existence checks).
Tags
Return values
ResponseInterface —Returns an HTTP response object indicating the result of the operation.
On success, the body contains the _key of the deleted document(s).
On failure, it returns a formatted error response (400, 404, or 500).