Elgg  Version master
Public Member Functions | Protected Attributes | List of all members
Elgg\Ajax\Service Class Reference

Models the Ajax API service. More...

Public Member Functions

 __construct (protected EventsService $events, protected SystemMessagesService $msgs, protected Request $request, protected ESMService $esm)
 Constructor. More...
 
 isAjax2Request ()
 Did the request come from the elgg/Ajax module? More...
 
 isReady ()
 Is the service ready to respond to the request? More...
 
 decodeJson ($string)
 Attempt to JSON decode the given string. More...
 
 respondFromOutput ($output, string $event_type= '', bool $try_decode=true)
 Send a JSON HTTP response with the given output. More...
 
 respondFromApiResponse (AjaxResponse $api_response, string $event_type= '')
 Send a JSON HTTP response based on the given API response. More...
 
 respondWithError (string $msg= '', int $status=400)
 Send a JSON HTTP 400 response. More...
 
 prepareResponse (\Elgg\Event $event)
 Prepare the response with additional metadata, like system messages and required ES modules. More...
 
 registerView (string $view)
 Register a view to be available for ajax calls. More...
 
 unregisterView (string $view)
 Unregister a view for ajax calls. More...
 
 getViews ()
 Returns an array of views allowed for ajax calls. More...
 

Protected Attributes

bool $response_sent = false
 
array $allowed_views = []
 

Detailed Description

Models the Ajax API service.

Since
1.12.0

Definition at line 19 of file Service.php.

Constructor & Destructor Documentation

Elgg\Ajax\Service::__construct ( protected EventsService  $events,
protected SystemMessagesService  $msgs,
protected Request  $request,
protected ESMService  $esm 
)

Constructor.

Parameters
EventsService$eventsEvents service
SystemMessagesService$msgsSystem messages service
Request$requestHttp Request
ESMService$esmESM service

Definition at line 33 of file Service.php.

Member Function Documentation

Elgg\Ajax\Service::decodeJson (   $string)

Attempt to JSON decode the given string.

Parameters
mixed$stringOutput string
Returns
mixed

Definition at line 73 of file Service.php.

Elgg\Ajax\Service::getViews ( )

Returns an array of views allowed for ajax calls.

Returns
string[]

Definition at line 257 of file Service.php.

Elgg\Ajax\Service::isAjax2Request ( )

Did the request come from the elgg/Ajax module?

Returns
bool

Definition at line 48 of file Service.php.

Elgg\Ajax\Service::isReady ( )

Is the service ready to respond to the request?

Some code paths involve multiple layers of handling (e.g. router calls actions/ajax views) so we must check whether the response has already been sent to avoid sending it twice. We can't use headers_sent() because Router needs to use output buffering.

Returns
bool

Definition at line 62 of file Service.php.

Elgg\Ajax\Service::prepareResponse ( \Elgg\Event  $event)

Prepare the response with additional metadata, like system messages and required ES modules.

Parameters
\Elgg\Event$event"ajax_response", "all"
Returns
AjaxResponse

Definition at line 206 of file Service.php.

Elgg\Ajax\Service::registerView ( string  $view)

Register a view to be available for ajax calls.

Parameters
string$viewThe view name
Returns
void

Definition at line 237 of file Service.php.

Elgg\Ajax\Service::respondFromApiResponse ( AjaxResponse  $api_response,
string  $event_type = '' 
)

Send a JSON HTTP response based on the given API response.

Parameters
AjaxResponse$api_responseAPI response
string$event_typeThe event type. If given, the response will be filtered by event
Returns
JsonResponse|false

Definition at line 120 of file Service.php.

Elgg\Ajax\Service::respondFromOutput (   $output,
string  $event_type = '',
bool  $try_decode = true 
)

Send a JSON HTTP response with the given output.

Parameters
mixed$outputOutput from a page/action handler
string$event_typeThe event type. If given, the response will be filtered by event
bool$try_decodeTry to convert a JSON string back to an abject
Returns
JsonResponse|false

Definition at line 91 of file Service.php.

Elgg\Ajax\Service::respondWithError ( string  $msg = '',
int  $status = 400 
)

Send a JSON HTTP 400 response.

Parameters
string$msgThe error message (not displayed to the user)
int$statusThe HTTP status code
Returns
JsonResponse|false

Definition at line 136 of file Service.php.

Elgg\Ajax\Service::unregisterView ( string  $view)

Unregister a view for ajax calls.

Parameters
string$viewThe view name
Returns
void

Definition at line 248 of file Service.php.

Member Data Documentation

array Elgg\Ajax\Service::$allowed_views = []
protected

Definition at line 23 of file Service.php.

bool Elgg\Ajax\Service::$response_sent = false
protected

Definition at line 21 of file Service.php.


The documentation for this class was generated from the following file: