phpDocumentor

AbstractApiV2Controller
in package

Class AbstractApiV2Controller

This class defines the inner core functionality of a ApiV2Controller. It contains the initialization and request validation functionality that every controller must have.

The functionality of this class is mark as private because child controllers must not alter the state at this point but rather adjust to it. This will force them to follow the same principles and methodologies.

Child API controllers can use the "init" method to initialize their common dependencies.

Tags
category

System

Table of Contents

DEFAULT_CONTROLLER_NAME  = 'DefaultApiV2Controller'
Default controller to be loaded when no resource was selected.
DEFAULT_PAGE_ITEMS  = 50
Defines the default page offset for responses that return multiple items.
DEFAULT_RATE_LIMIT  = 5000
Defines the maximum request limit for an authorized client.
DEFAULT_RATE_RESET_PERIOD  = 15
Defines the duration of an API session in minutes.
$request  : Request
$response  : Response
$uri  : array<string|int, mixed>
Contains the request URI segments after the root api version segment.
__construct()  : mixed
AbstractApiV2Controller Constructor
getResponse()  : Response
_prepareResponse()  : mixed
[PRIVATE] Prepare response headers.
_setRateLimitHeader()  : mixed
[PRIVATE] Handle rate limit headers.
_validateRequest()  : mixed
[PRIVATE] Validate request before proceeding with response.
getRootUri()  : string

Constants

DEFAULT_CONTROLLER_NAME

Default controller to be loaded when no resource was selected.

public string DEFAULT_CONTROLLER_NAME = 'DefaultApiV2Controller'

DEFAULT_PAGE_ITEMS

Defines the default page offset for responses that return multiple items.

public int DEFAULT_PAGE_ITEMS = 50

DEFAULT_RATE_RESET_PERIOD

Defines the duration of an API session in minutes.

public int DEFAULT_RATE_RESET_PERIOD = 15

Properties

$uri

Contains the request URI segments after the root api version segment.

protected array<string|int, mixed> $uri

Example: URI - api.php/v2/customers/73/addresses CODE - $this->uri[1]; // will return '73'

Methods

__construct()

AbstractApiV2Controller Constructor

public __construct(Request $request, Response $response, array<string|int, mixed> $uri) : mixed

Call this constructor from every child controller class in order to set the Slim instance and the request routes arguments to the class.

Parameters
$request : Request
$response : Response
$uri : array<string|int, mixed>

This array contains all the segments of the current request, starting from the resource.

Tags
throws
HttpApiV2Exception

Through _validateRequest

deprecated

The "__initialize" method will is deprecated and will be removed in a future version. Please use the new "init" for bootstrapping your child API controllers.

Return values
mixed

_prepareResponse()

[PRIVATE] Prepare response headers.

protected _prepareResponse() : mixed

This method will prepare default attributes of the API responses. Further response settings must be set explicitly from each controller method separately.

Not available to child-controllers (private method).

Return values
mixed

_setRateLimitHeader()

[PRIVATE] Handle rate limit headers.

protected _setRateLimitHeader() : mixed

There is a cache file that will store each user session and provide a security mechanism that will protect the shop from DOS attacks or service overuse. Each session will use the hashed "Authorization header" to identify the client. When the limit is reached a "HTTP/1.1 429 Too Many Requests" will be returned.

Headers: X-Rate-Limit-Limit >> Max number of requests allowed. X-Rate-Limit-Remaining >> Number of requests remaining. X-Rate-Limit-Reset >> UTC epoch seconds until the limit is reset.

Important: This method will be executed in every API call and it might slow the response time due to filesystem operations. If the difference is significant then it should be optimized.

Not available to child-controllers (private method).

Tags
throws
HttpApiV2Exception

If request limit exceed - 429 Too Many Requests

Return values
mixed

_validateRequest()

[PRIVATE] Validate request before proceeding with response.

protected _validateRequest() : mixed

This method will validate the request headers, user authentication and other parameters before the controller proceeds with the response.

Not available to child-controllers (private method).

Tags
throws
HttpApiV2Exception

If validation fails - 415 Unsupported media type.

Return values
mixed

Search results