AjaxController
extends AbstractActionController
in package
implements
TranslatorAwareInterface
Uses
AjaxResponseTrait, TranslatorAwareTrait
This controller handles global AJAX functionality
Tags
Interfaces, Classes, Traits and Enums
- TranslatorAwareInterface
- Lightweight translator aware marker interface (used as an alternative to \Laminas\I18n\Translator\TranslatorAwareInterface, which requires an excessive number of methods to be implemented).
Table of Contents
- $ajaxManager : PluginManager
- AJAX Handler plugin manager
- $translator : TranslatorInterface
- Translator
- __construct() : mixed
- Constructor
- getTranslator() : TranslatorInterface
- Get translator object.
- getTranslatorLocale() : string
- Get the locale from the translator.
- jsonAction() : Response
- Make an AJAX call with a JSON-formatted response.
- recommendAction() : Response
- Load a recommendation module via AJAX.
- setTranslator() : TranslatorAwareInterface
- Set a translator
- systemStatusAction() : Response
- Check status and return a status message for e.g. a load balancer.
- translate() : string
- Translate a string (or string-castable object)
- translateWithPrefix() : string
- Translate a string (or string-castable object) using a prefix, or without the prefix if a prefixed translation is not found.
- callAjaxMethod() : Response
- Call an AJAX method and turn the result into a response.
- extractTextDomain() : array<string|int, mixed>
- Given a translation string with or without a text domain, return an array with the raw string and the text domain separated.
- formatContent() : string
- Format the content of the AJAX response based on the response type.
- getAjaxResponse() : Response
- Send output data and exit.
- getDebugTranslation() : string
- Build a debug-mode translation
- getExceptionResponse() : Response
- Turn an exception into error response.
- sanitizeTranslationKey() : string
- Make sure there are not any illegal characters in the translation key that might prevent successful lookup in language files.
- translateString() : string
- Get translation for a string
Properties
$ajaxManager
AJAX Handler plugin manager
protected
PluginManager
$ajaxManager
= null
$translator
Translator
protected
TranslatorInterface
$translator
= null
Methods
__construct()
Constructor
public
__construct(PluginManager $am) : mixed
Parameters
- $am : PluginManager
-
AJAX Handler Plugin Manager
Return values
mixed —getTranslator()
Get translator object.
public
getTranslator() : TranslatorInterface
Return values
TranslatorInterface —getTranslatorLocale()
Get the locale from the translator.
public
getTranslatorLocale([string $default = 'en' ]) : string
Parameters
- $default : string = 'en'
-
Default to use if translator absent.
Return values
string —jsonAction()
Make an AJAX call with a JSON-formatted response.
public
jsonAction() : Response
Return values
Response —recommendAction()
Load a recommendation module via AJAX.
public
recommendAction() : Response
Return values
Response —setTranslator()
Set a translator
public
setTranslator(TranslatorInterface $translator) : TranslatorAwareInterface
Parameters
- $translator : TranslatorInterface
-
Translator
Return values
TranslatorAwareInterface —systemStatusAction()
Check status and return a status message for e.g. a load balancer.
public
systemStatusAction() : Response
A simple OK as text/plain is returned if everything works properly.
Return values
Response —translate()
Translate a string (or string-castable object)
public
translate(string|object|array<string|int, mixed> $target[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, bool $useIcuFormatter = false ][, array<string|int, string> $fallbackDomains = [] ]) : string
Parameters
- $target : string|object|array<string|int, mixed>
-
String to translate or an array of text domain and string to translate
- $tokens : array<string|int, mixed> = []
-
Tokens to inject into the translated string
- $default : string = null
-
Default value to use if no translation is found (null for no default).
- $useIcuFormatter : bool = false
-
Should we use an ICU message formatter instead of the default behavior?
- $fallbackDomains : array<string|int, string> = []
-
Text domains to check if no match is found in the domain specified in $target
Return values
string —translateWithPrefix()
Translate a string (or string-castable object) using a prefix, or without the prefix if a prefixed translation is not found.
public
translateWithPrefix(string $prefix, string|object|array<string|int, mixed> $target[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, bool $useIcuFormatter = false ][, array<string|int, string> $fallbackDomains = [] ]) : string
Parameters
- $prefix : string
-
Translation key prefix
- $target : string|object|array<string|int, mixed>
-
String to translate or an array of text domain and string to translate
- $tokens : array<string|int, mixed> = []
-
Tokens to inject into the translated string
- $default : string = null
-
Default value to use if no translation is found (null for no default).
- $useIcuFormatter : bool = false
-
Should we use an ICU message formatter instead of the default behavior?
- $fallbackDomains : array<string|int, string> = []
-
Text domains to check if no match is found in the domain specified in $target
Return values
string —callAjaxMethod()
Call an AJAX method and turn the result into a response.
protected
callAjaxMethod(string $method[, string $type = 'application/json' ]) : Response
Parameters
- $method : string
-
AJAX method to call
- $type : string = 'application/json'
-
Content type to output
Return values
Response —extractTextDomain()
Given a translation string with or without a text domain, return an array with the raw string and the text domain separated.
protected
extractTextDomain(string|object|array<string|int, mixed> $target) : array<string|int, mixed>
Parameters
- $target : string|object|array<string|int, mixed>
-
String to translate or an array of text domain and string to translate
Return values
array<string|int, mixed> —formatContent()
Format the content of the AJAX response based on the response type.
protected
formatContent(string $type, mixed $data, int $httpCode) : string
Parameters
- $type : string
-
Content-type of output
- $data : mixed
-
The response data
- $httpCode : int
-
A custom HTTP Status Code
Tags
Return values
string —getAjaxResponse()
Send output data and exit.
protected
getAjaxResponse(string $type, mixed $data[, int $httpCode = null ]) : Response
Parameters
- $type : string
-
Content type to output
- $data : mixed
-
The response data
- $httpCode : int = null
-
A custom HTTP Status Code
Tags
Return values
Response —getDebugTranslation()
Build a debug-mode translation
protected
getDebugTranslation(string $domain, string $str, array<string|int, mixed> $tokens) : string
Parameters
- $domain : string
-
Text domain
- $str : string
-
String to translate
- $tokens : array<string|int, mixed>
-
Tokens to inject into the translated string
Return values
string —getExceptionResponse()
Turn an exception into error response.
protected
getExceptionResponse(string $type, Exception $e) : Response
Parameters
- $type : string
-
Content type to output
- $e : Exception
-
Exception to output.
Return values
Response —sanitizeTranslationKey()
Make sure there are not any illegal characters in the translation key that might prevent successful lookup in language files.
protected
sanitizeTranslationKey(string $key) : string
Parameters
- $key : string
-
Key to sanitize
Return values
string —Sanitized key
translateString()
Get translation for a string
protected
translateString(string $rawStr[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, string $domain = 'default' ][, bool $useIcuFormatter = false ]) : string
Parameters
- $rawStr : string
-
String to translate
- $tokens : array<string|int, mixed> = []
-
Tokens to inject into the translated string
- $default : string = null
-
Default value to use if no translation is found (null for no default).
- $domain : string = 'default'
-
Text domain (omit for default)
- $useIcuFormatter : bool = false
-
Should we use an ICU message formatter instead of the default behavior?