AbstractApiV2Controller

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.

category

System

package

ApiV2Controllers

Methods

AbstractApiV2Controller Constructor

__construct(\Slim\Slim $api, array $uri) 

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

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.

Throws
\HttpApiV2Exception

Through _validateRequest

Arguments

$api

\Slim\Slim

Slim framework instance, used for request/response manipulation.

$uri

array

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

[PRIVATE] Prepare response headers.

_prepareResponse() 

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).

[PRIVATE] Handle rate limit headers.

_setRateLimitHeader() 

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).

Throws
\HttpApiV2Exception

If request limit exceed - 429 Too Many Requests

[PRIVATE] Validate request before proceeding with response.

_validateRequest() 

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).

Throws
\HttpApiV2Exception

If validation fails - 415 Unsupported media type.

Constants

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

DEFAULT_PAGE_ITEMS
var

Default controller to be loaded when no resource was selected.

DEFAULT_CONTROLLER_NAME
var

Defines the maximum request limit for an authorized client.

DEFAULT_RATE_LIMIT
var

Defines the duration of an API session in minutes.

DEFAULT_RATE_RESET_PERIOD
var

Properties

Slim Framework instance is used to manipulate the request or response data.

api : \Slim\Slim
var

Type(s)

\Slim\Slim

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

uri : array

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

var

Type(s)

array