### Install laminas-api-tools/api-tools-rest using Composer Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Installs the api-tools-rest module using the Composer package manager. This command fetches and installs the specified package and its dependencies. ```console $ composer require laminas-api-tools/api-tools-rest ``` -------------------------------- ### User Configuration Example for RestController Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Example of how to configure a REST controller, specifying listener, route details, HTTP methods, pagination, and entity/collection classes. ```php 'AddressBook\\V1\\Rest\\Contact\\Controller' => [ 'listener' => 'AddressBook\\V1\\Rest\\Contact\\ContactResource', 'route_name' => 'address-book.rest.contact', 'route_identifier_name' => 'contact_id', 'collection_name' => 'contact', 'entity_http_methods' => [ 0 => 'GET', 1 => 'PATCH', 2 => 'PUT', 3 => 'DELETE', ], 'collection_http_methods' => [ 0 => 'GET', 1 => 'POST', ], 'collection_query_whitelist' => [], 'page_size' => 25, 'page_size_param' => null, 'entity_class' => 'AddressBook\\V1\\Rest\\Contact\\ContactEntity', 'collection_class' => 'AddressBook\\V1\\Rest\\Contact\\ContactCollection', 'service_name' => 'Contact', ] ``` -------------------------------- ### AbstractResourceListener Methods for REST Operations (PHP) Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md This class provides a base implementation for REST resource listeners. It defines methods that are triggered by various HTTP methods (POST, DELETE, GET, PATCH, PUT) for both resource entities and collections. These methods are called by the RestController via the Resource object. ```PHP create($data) delete($id) deleteList($data) fetch($id) fetchAll($params = []) patch($id, $data) patchList($data) update($id, $data) replaceList($data) ``` -------------------------------- ### api-tools-rest Module System Configuration Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Core system configuration for the api-tools-rest module, including service manager definitions for listeners and controllers, and view manager settings. ```php 'service_manager' => [ 'invokables' => [ 'Laminas\\ApiTools\\Rest\\RestParametersListener' => 'Laminas\\ApiTools\\Rest\\Listener\\RestParametersListener', ], 'factories' => [ 'Laminas\\ApiTools\\Rest\\OptionsListener' => 'Laminas\\ApiTools\\Rest\\Factory\\OptionsListenerFactory', ], ], 'controllers' => [ 'abstract_factories' => [ 'Laminas\\ApiTools\\Rest\\Factory\\RestControllerFactory', ], ], 'view_manager' => [ // Enable this in your application configuration in order to get full // exception stack traces in your API-Problem responses. 'display_exceptions' => false, ] ``` -------------------------------- ### System Configuration Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Provides the necessary system configuration for the api-tools-rest module to function within a Laminas Framework application. ```APIDOC ## System Configuration The `api-tools-rest` module requires specific configuration within the Laminas Framework application to ensure proper operation. ### `service_manager` Configuration ```php 'service_manager' => [ 'invokables' => [ 'Laminas\ApiTools\Rest\RestParametersListener' => 'Laminas\ApiTools\Rest\Listener\RestParametersListener', ], 'factories' => [ 'Laminas\ApiTools\Rest\OptionsListener' => 'Laminas\ApiTools\Rest\Factory\OptionsListenerFactory', ], ], ``` ### `controllers` Configuration ```php 'controllers' => [ 'abstract_factories' => [ 'Laminas\ApiTools\Rest\Factory\RestControllerFactory', ], ], ``` ### `view_manager` Configuration ```php 'view_manager' => [ // Enable this in your application configuration to get full exception stack traces // in your API-Problem responses. 'display_exceptions' => false, ], ``` ``` -------------------------------- ### API Tools Rest Configuration Options Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Describes the configuration options available for the api-tools-rest module, used to define RESTful API resources. ```APIDOC ## API Tools Rest Configuration Options This section outlines the various configuration options that can be used to define a RESTful resource within the Laminas API Tools framework. ### Configuration Keys * **`controller_class`** (optional) * Description: An alternate controller class to use. Must extend `Laminas\ApiTools\Rest\RestController`. * **`identifier`** (optional) * Description: The event identifier name for the controller. * **`resource_identifiers`** (optional) * Description: The event identifier name or an array of names for the resource. * **`entity_class`** * Description: The class used for representing an entity. Useful for introspection. * **`route_name`** * Description: The route name associated with this REST service. Used for generating links. * **`route_identifier_name`** * Description: The parameter name for the identifier in the route specification. * **`listener`** * Description: The resource class that handles collection or entity requests. * **`page_size`** * Description: The number of entities to return per page for paginated collections. * **`max_page_size`** (optional) * Description: The maximum number of entities allowed per page. Helps prevent DoS attacks. * **`min_page_size`** (optional) * Description: The minimum number of entities allowed per page. * **`page_size_param`** (optional) * Description: The name of a query string parameter to set the per-request page size. ### User Configuration Example ```php 'AddressBook\\V1\\Rest\\Contact\\Controller' => [ 'listener' => 'AddressBook\\V1\\Rest\\Contact\\ContactResource', 'route_name' => 'address-book.rest.contact', 'route_identifier_name' => 'contact_id', 'collection_name' => 'contact', 'entity_http_methods' => [ 0 => 'GET', 1 => 'PATCH', 2 => 'PUT', 3 => 'DELETE', ], 'collection_http_methods' => [ 0 => 'GET', 1 => 'POST', ], 'collection_query_whitelist' => [], 'page_size' => 25, 'page_size_param' => null, 'entity_class' => 'AddressBook\\V1\\Rest\\Contact\\ContactEntity', 'collection_class' => 'AddressBook\\V1\\Rest\\Contact\\ContactCollection', 'service_name' => 'Contact', ] ``` ``` -------------------------------- ### Laminas REST Module Configuration Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Configuration options for the api-tools-rest module, including controller service names and allowed HTTP methods for collection and entity requests. ```APIDOC ## Laminas REST Module Configuration The `api-tools-rest` module is configured via the top-level `api-tools-rest` key in your application's configuration. ### Configuration Structure ``` [ "api-tools-rest" => [ "ControllerServiceName" => [ "collection_http_methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"], "entity_http_methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"], "collection_name" => "collection", "collection_query_whitelist" => ["sort", "filter", "page", "limit"] ] ] ] ``` ### Configuration Keys - **ControllerServiceName**: A unique name for your REST controller. This will be used as the service name to fetch the controller. - **`collection_http_methods`**: An array of HTTP methods allowed for collection requests (e.g., GET all, POST new). - **`entity_http_methods`**: An array of HTTP methods allowed for entity-specific requests (e.g., GET one, PUT update). - **`collection_name`**: The name of the property used to represent the collection in HAL responses. - **`collection_query_whitelist`** (optional): An array of query string parameters that will be passed to the resource's `fetchAll()` method and included in collection links. Starting in v1.5.0, these are merged with input filter keys for the GET method. ``` -------------------------------- ### Resource Listener Methods Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md The AbstractResourceListener provides base implementations for resource operations. The specific methods called depend on the HTTP method and the target of the request (entity or collection). ```APIDOC ## Resource Listener Methods ### Description This section outlines the methods available in `Laminas\ApiTools\Rest\AbstractResourceListener` that are triggered by different HTTP methods when interacting with REST resources. ### Methods - **create($data)** - **Description**: Triggered by a `POST` request to a resource collection. - **HTTP Method**: POST - **Target**: Resource Collection - **delete($id)** - **Description**: Triggered by a `DELETE` request to a resource entity. - **HTTP Method**: DELETE - **Target**: Resource Entity - **deleteList($data)** - **Description**: Triggered by a `DELETE` request to a resource collection. - **HTTP Method**: DELETE - **Target**: Resource Collection - **fetch($id)** - **Description**: Triggered by a `GET` request to a resource entity. - **HTTP Method**: GET - **Target**: Resource Entity - **fetchAll($params = [])** - **Description**: Triggered by a `GET` request to a resource collection. - **HTTP Method**: GET - **Target**: Resource Collection - **patch($id, $data)** - **Description**: Triggered by a `PATCH` request to a resource entity. - **HTTP Method**: PATCH - **Target**: Resource Entity - **patchList($data)** - **Description**: Triggered by a `PATCH` request to a resource collection. - **HTTP Method**: PATCH - **Target**: Resource Collection - **update($id, $data)** - **Description**: Triggered by a `PUT` request to a resource entity. - **HTTP Method**: PUT - **Target**: Resource Entity - **replaceList($data)** - **Description**: Triggered by a `PUT` request to a resource collection. - **HTTP Method**: PUT - **Target**: Resource Collection ``` -------------------------------- ### Add api-tools-rest to composer.json Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Manually adds the api-tools-rest dependency to the `composer.json` file, specifying the version constraint. After modification, `composer update` must be run. ```javascript "require": { "laminas-api-tools/api-tools-rest": "^1.3" } ``` -------------------------------- ### Register Laminas REST API Tools module Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Registers the Laminas REST API Tools module within the application's configuration file (`config/application.config.php`). This ensures the module is loaded and available for use. ```php return [ /* ... */ 'modules' => [ /* ... */ 'Laminas\ApiTools\Rest', ], /* ... */ ]; ``` -------------------------------- ### Laminas Events Listeners Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md Details the event listeners provided by the api-tools-rest module for handling requests and managing API behavior. ```APIDOC ## Laminas Events Listeners The `api-tools-rest` module utilizes several listeners to manage API behavior and respond to requests. ### `Laminas\ApiTools\Rest\Listener\OptionsListener` * **Event:** `MvcEvent::EVENT_ROUTE` (priority: -100) * **Purpose:** * Handles unsupported HTTP methods for REST entities or collections by returning a `405 Method not allowed` response with an `Allow` header. * Responds to `OPTIONS` requests with a `200 OK` status and an `Allow` header listing supported methods. ### `Laminas\ApiTools\Rest\Listener\RestParametersListener` * **Event:** `dispatch` (priority: 100) * **Purpose:** Maps query string arguments from the request to the `Resource` object within the `RestController` and injects `RouteMatch` information. ``` -------------------------------- ### RestController Functionality Source: https://github.com/laminas-api-tools/api-tools-rest/blob/1.9.x/README.md The RestController is the base controller for REST services, delegating operations to a Resource object and returning HAL payloads or API Problem responses. ```APIDOC ## RestController Functionality ### Description `Laminas\ApiTools\Rest\RestController` is the foundational controller for RESTful services within ApiTools. It routes incoming HTTP requests to the appropriate methods within a `Laminas\ApiTools\Rest\Resource` object. Successful operations return data formatted using HAL, while errors are communicated via API Problem responses. ### Controller Class - **Class**: `Laminas\ApiTools\Rest\RestController` ### Factory - **Factory**: `Laminas\ApiTools\Rest\Factory\RestControllerFactory` ### Behavior - Dispatches business logic to `Laminas\ApiTools\Rest\Resource` based on the HTTP method. - Returns HAL payloads for successful responses. - Returns API Problem responses for error conditions. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.