class ApiController

Base class for all Api controllers

Traits

Controller trait
Multilanguage trait
UserAccess trait
ApiResponse trait
Errors trait

Properties

protected string|null $extensionName Extension name from BaseController
protected Container|null $container Container from BaseController
protected string|null $pageName Page name from BaseController
protected array $params Controller params from BaseController
protected array $messages Response messages from Multilanguage
protected string $messagesComponentName Messages component name from Multilanguage
protected boolean $messagesLoaded Messages loaded from Multilanguage
protected array $result Response result from ApiResponse
protected bool $prettyFormat pretty format json from ApiResponse
protected Closure|null $dataValidCallback Data validatin callback from Errors
protected Closure|null $dataErrorCallback Data error callback from Errors
protected array|null $validationErrorMessages Validation error messages from Errors
protected array $errors Errors list from Errors
protected string $modelClass Model class name

Methods

string|false
getRouteUrl(string|null $routeName, string|null $extension, array $params = [], string|null $language = null, bool $relative = false)

Get page url

string
getPageUrl(string $path = '', bool $relative = false, string|null $language = null)

Get page url

array
getParams()

Get params

mixed|null
getParam(string $key, mixed|null $default = null)

Get param

bool
resolveRouteParams(Request $request)

Resolve route params

string|null
getPageName()

Get page name

void
setExtensionName(string|null $name)

Set extension name

string|null
getExtensionName()

Get extension name

mixed
get(string $id)

Get item from container

bool
has(string $id)

Return tru if container item esist

Container
getContainer()

Get container

bool
logInfo(string $message, array $context = [])

Log message

array
getRequestParams(Request $request)

Get request params

mixed
getQueryParam(ServerRequestInterface $request, string $name, mixed $default = null)

Get query param

array
resolveRequestParams(Request $request, array $paramsKeys)

Resolve params

string
getUrl(ServerRequestInterface $request, bool $relative = false)

Get url

string|null
getError(string $errorCode, array $params = [])

Get error

bool
logError(string $message, array $context = [])

Log error

string|null
getMessage(string $name)

Get message

void
loadMessages(string $componentName)

Load messages from html component json file

void
loadMesasgesComponent(string|null $language = null)

Load messages component

string
getPageLanguage(array $data = [])

Get page language

bool
isLanguageChange(array $data)

Return true if page load is with new language code

string
getDefaultLanguage()

Get default language

void
requireAccess(string $name, mixed $type = null, object|null $response = null)

Reguire permission check if current user have permission

bool
hasControlPanelAccess()

Return true if user have control panel access

bool
hasAccess(string $name, string $type = null)

Return true if user have access permission

mixed
requireControlPanelPermission(object|null $response = null)

Require control panel permission

mixed
user()

Return current logged user

int|null
getUserId()

Return current logged user id

ResponseInterface
getResponse(bool $raw = false)

Return response

ApiController
message(string $name)

Add message to response, first find in messages array if not found display name value as message

Self
field(string $name, mixed $value)

Set result field

Self
useJsonPrettyformat()

Set json pretty format to true

mixed
setResponse(bool $condition, array|string|Closure $data, string|string|Closure $error)

Set response

Self
setResult(mixed $data)

Set response result

void
clearResult()

Clear result

void
setResultField(string $name, mixed $value)

Set field to result array

void
setResultFields(array $values, string|null $filedName = null)

Set result filelds

string
getResponseJson(bool $raw = false)

Return json

void
setErrors(array $errors)

Set errors

from Errors
ApiController
error(string $name, array $params = [])

Set error, first find in messages array if not found display name value as error

from Errors
void
addError(string $errorCode)

Add system error

from Errors
string|null
getValidationErrorMessage(string $code)

Get validaiton error message

from Errors
void
addErrors(array $errors)

Add errors

from Errors
int
getErrorCount()

Return errors count

from Errors
bool
hasError()

Return true if response have error

from Errors
Self
withError(string $errorMessage, bool $condition = true)

Set error message

from Errors
void
setError(string $errorMessage, bool $condition = true)

Set error message

from Errors
void
clearErrors()

Clear all errors.

from Errors
void
onValidationError(Closure $callback)

Set callback for validation errors

from Errors
void
onDataValid(Closure $callback)

Set callback for validation done

from Errors
Closure|null
getDataValidCallback()

Get data validation callback

from Errors
Closure|null
getValidationErrorCallback()

Get validation error callback

from Errors
void
loadValidationErrors()

Load validation error messages

from Errors
array
resolveValidationErrors(array $errors)

Resolve validation errors

from Errors
__construct(Container $container = null)

Constructor

void
init()

Init controller, override this method in child classes

mixed|false
dispatch(string $eventName, array $params)

Dispatch event

void
setModelClass(string $class)

Set model class name

string|null
getModelClass()

Get model class name

mixed
__call(string $name, array $arguments)

Run {method name}Controller function if exist

Details

in BaseController at line 59
string|false getRouteUrl(string|null $routeName, string|null $extension, array $params = [], string|null $language = null, bool $relative = false)

Get page url

Parameters

string|null $routeName
string|null $extension
array $params
string|null $language
bool $relative

Return Value

string|false

in BaseController at line 88
string getPageUrl(string $path = '', bool $relative = false, string|null $language = null)

Get page url

Parameters

string $path
bool $relative
string|null $language

Return Value

string

in BaseController at line 98
array getParams()

Get params

Return Value

array

in BaseController at line 110
mixed|null getParam(string $key, mixed|null $default = null)

Get param

Parameters

string $key
mixed|null $default

Return Value

mixed|null

in BaseController at line 121
protected bool resolveRouteParams(Request $request)

Resolve route params

Parameters

Request $request

Return Value

bool

in BaseController at line 146
string|null getPageName()

Get page name

Return Value

string|null

in BaseController at line 157
void setExtensionName(string|null $name)

Set extension name

Parameters

string|null $name

Return Value

void

in BaseController at line 167
string|null getExtensionName()

Get extension name

Return Value

string|null

in BaseController at line 178
mixed get(string $id)

Get item from container

Parameters

string $id

Return Value

mixed

in BaseController at line 194
bool has(string $id)

Return tru if container item esist

Parameters

string $id

Return Value

bool

in BaseController at line 204
Container getContainer()

Get container

Return Value

Container

in BaseController at line 216
bool logInfo(string $message, array $context = [])

Log message

Parameters

string $message
array $context

Return Value

bool

in BaseController at line 227
array getRequestParams(Request $request)

Get request params

Parameters

Request $request

Return Value

array

in BaseController at line 244
mixed getQueryParam(ServerRequestInterface $request, string $name, mixed $default = null)

Get query param

Parameters

ServerRequestInterface $request
string $name
mixed $default

Return Value

mixed

in BaseController at line 258
array resolveRequestParams(Request $request, array $paramsKeys)

Resolve params

Parameters

Request $request
array $paramsKeys

Return Value

array

in BaseController at line 275
string getUrl(ServerRequestInterface $request, bool $relative = false)

Get url

Parameters

ServerRequestInterface $request
bool $relative

Return Value

string

in BaseController at line 289
string|null getError(string $errorCode, array $params = [])

Get error

Parameters

string $errorCode
array $params

Return Value

string|null

in BaseController at line 303
bool logError(string $message, array $context = [])

Log error

Parameters

string $message
array $context

Return Value

bool

in Multilanguage at line 50
string|null getMessage(string $name)

Get message

Parameters

string $name

Return Value

string|null

in Multilanguage at line 66
void loadMessages(string $componentName)

Load messages from html component json file

Parameters

string $componentName

Return Value

void

in Multilanguage at line 78
protected void loadMesasgesComponent(string|null $language = null)

Load messages component

Parameters

string|null $language

Return Value

void

in Multilanguage at line 106
string getPageLanguage(array $data = [])

Get page language

Parameters

array $data

Return Value

string

in Multilanguage at line 129
bool isLanguageChange(array $data)

Return true if page load is with new language code

Parameters

array $data

Return Value

bool

in Multilanguage at line 143
string getDefaultLanguage()

Get default language

Return Value

string

in UserAccess at line 25
void requireAccess(string $name, mixed $type = null, object|null $response = null)

Reguire permission check if current user have permission

Parameters

string $name
mixed $type
object|null $response

Return Value

void

in UserAccess at line 43
bool hasControlPanelAccess()

Return true if user have control panel access

Return Value

bool

in UserAccess at line 55
bool hasAccess(string $name, string $type = null)

Return true if user have access permission

Parameters

string $name
string $type

Return Value

bool

in UserAccess at line 66
mixed requireControlPanelPermission(object|null $response = null)

Require control panel permission

Parameters

object|null $response

Return Value

mixed

in UserAccess at line 80
mixed user()

Return current logged user

Return Value

mixed

in UserAccess at line 90
int|null getUserId()

Return current logged user id

Return Value

int|null

in ApiResponse at line 37
ResponseInterface getResponse(bool $raw = false)

Return response

Parameters

bool $raw

Return Value

ResponseInterface

in ApiResponse at line 55
ApiController message(string $name)

Add message to response, first find in messages array if not found display name value as message

Parameters

string $name

Return Value

ApiController

in ApiResponse at line 72
Self field(string $name, mixed $value)

Set result field

Parameters

string $name
mixed $value

Return Value

Self

in ApiResponse at line 84
Self useJsonPrettyformat()

Set json pretty format to true

Return Value

Self

in ApiResponse at line 99
mixed setResponse(bool $condition, array|string|Closure $data, string|string|Closure $error)

Set response

Parameters

bool $condition
array|string|Closure $data
string|string|Closure $error

Return Value

mixed

in ApiResponse at line 124
Self setResult(mixed $data)

Set response result

Parameters

mixed $data

Return Value

Self

in ApiResponse at line 136
void clearResult()

Clear result

Return Value

void

in ApiResponse at line 153
void setResultField(string $name, mixed $value)

Set field to result array

Parameters

string $name
mixed $value

Return Value

void

in ApiResponse at line 165
void setResultFields(array $values, string|null $filedName = null)

Set result filelds

Parameters

array $values
string|null $filedName

Return Value

void

in ApiResponse at line 182
string getResponseJson(bool $raw = false)

Return json

Parameters

bool $raw

Return Value

string

in Errors at line 54
void setErrors(array $errors)

Set errors

Parameters

array $errors

Return Value

void

in Errors at line 66
ApiController error(string $name, array $params = [])

Set error, first find in messages array if not found display name value as error

Parameters

string $name
array $params

Return Value

ApiController

in Errors at line 85
void addError(string $errorCode)

Add system error

Parameters

string $errorCode

Return Value

void

in Errors at line 99
protected string|null getValidationErrorMessage(string $code)

Get validaiton error message

Parameters

string $code

Return Value

string|null

in Errors at line 110
void addErrors(array $errors)

Add errors

Parameters

array $errors

Return Value

void

in Errors at line 120
int getErrorCount()

Return errors count

Return Value

int

in Errors at line 130
bool hasError()

Return true if response have error

Return Value

bool

in Errors at line 142
Self withError(string $errorMessage, bool $condition = true)

Set error message

Parameters

string $errorMessage
bool $condition

Return Value

Self

in Errors at line 156
void setError(string $errorMessage, bool $condition = true)

Set error message

Parameters

string $errorMessage
bool $condition

Return Value

void

in Errors at line 168
void clearErrors()

Clear all errors.

Return Value

void

in Errors at line 179
void onValidationError(Closure $callback)

Set callback for validation errors

Parameters

Closure $callback

Return Value

void

in Errors at line 190
void onDataValid(Closure $callback)

Set callback for validation done

Parameters

Closure $callback

Return Value

void

in Errors at line 200
Closure|null getDataValidCallback()

Get data validation callback

Return Value

Closure|null

in Errors at line 210
Closure|null getValidationErrorCallback()

Get validation error callback

Return Value

Closure|null

in Errors at line 220
void loadValidationErrors()

Load validation error messages

Return Value

void

in Errors at line 236
protected array resolveValidationErrors(array $errors)

Resolve validation errors

Parameters

array $errors

Return Value

array

at line 42
__construct(Container $container = null)

Constructor

Parameters

Container $container

at line 61
void init()

Init controller, override this method in child classes

Return Value

void

at line 72
mixed|false dispatch(string $eventName, array $params)

Dispatch event

Parameters

string $eventName
array $params

Return Value

mixed|false

at line 83
void setModelClass(string $class)

Set model class name

Parameters

string $class

Return Value

void

at line 93
string|null getModelClass()

Get model class name

Return Value

string|null

at line 105
mixed __call(string $name, array $arguments)

Run {method name}Controller function if exist

Parameters

string $name
array $arguments

Return Value

mixed