Class: JsonRequestHandler

abstract

Class for handling the JSON API requests.

Details

The user should derive from this class and implement the methods with the names of the API actions.

URL: http://smartfactorydev.loc/api/login/

Action: login

function login()
{
    if (empty($this->request_data["login"]) || empty($this->request_data["password"]) ||
        $this->request_data["login"] != "admin" || $this->request_data["password"] != "qwerty") {
        throw new SmartException("Wrong login or password!", "wrong_login");
    }

    $this->response_data["welcome_msg"] = "Welcome, admin!";
}

Extends

Authors

Oleg Schildt

Package

Source code

 

Properties
protected

Internal variable for storing the request data.

protected

Internal variable for storing the response data.

Methods
protected

This is an auxiliary function for parsing the incoming JSON data.

protected

This is an auxiliary function for sending the response in JSON format.


Property: JsonRequestHandler::$request_data

protected array $request_data = [];

Internal variable for storing the request data.

Type

array

Authors

Oleg Schildt

Source code


Property: JsonRequestHandler::$response_data

protected array $response_data = [];

Internal variable for storing the response data.

Type

array

Authors

Oleg Schildt

Source code


Method: JsonRequestHandler::parseInput()

protected function parseInput(): void;

This is an auxiliary function for parsing the incoming JSON data.

Details

It validates the content type 'application/json' and takes the data from RAWDATA.

Returns

void

Throws

\Exception|\SmartFactory\SmartException

It might throw an exception if the content type oк JSON data is invalid.

Overrides

Authors

Oleg Schildt

Source code


Method: JsonRequestHandler::sendResponse()

protected function sendResponse(): void;

This is an auxiliary function for sending the response in JSON format.

Details

It sends the collected response data and the response headers. The header "Content-type: application/json; charset=UTF-8" ist sent automatically.

Returns

void

Throws

\SmartFactory\SmartException

It might throw exceptions in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code