PHP Class Elgg\Ajax\Service

Since: 1.12.0
Mostra file Open project: elgg/elgg Class Usage Examples

Public Methods

Method Description
__construct ( PluginHooksService $hooks, SystemMessagesService $msgs, Input $input, Elgg\Amd\Config $amdConfig ) Constructor
appendDeps ( string $hook, string $type, Elgg\Services\AjaxResponse $response, array $params ) : Elgg\Services\AjaxResponse Send required AMD modules list back with the response
appendMessages ( string $hook, string $type, Elgg\Services\AjaxResponse $response, array $params ) : Elgg\Services\AjaxResponse Send system messages back with the response
decodeJson ( mixed $string ) : mixed Attempt to JSON decode the given string
getViews ( ) : string[] Returns an array of views allowed for ajax calls
isAjax2Request ( ) : boolean Did the request come from the elgg/Ajax module?
isReady ( ) : boolean Is the service ready to respond to the request?
registerView ( string $view ) : void Register a view to be available for ajax calls
respondFromApiResponse ( Elgg\Services\AjaxResponse $api_response, string $hook_type = '' ) : Symfony\Component\HttpFoundation\JsonResponse Send a JSON HTTP response based on the given API response
respondFromOutput ( mixed $output, string $hook_type = '', boolean $try_decode = true ) : Symfony\Component\HttpFoundation\JsonResponse Send a JSON HTTP response with the given output
respondWithError ( string $msg = '', integer $status = 400 ) : Symfony\Component\HttpFoundation\JsonResponse Send a JSON HTTP 400 response
unregisterView ( string $view ) : void Unregister a view for ajax calls

Private Methods

Method Description
buildHttpResponse ( Elgg\Services\AjaxResponse $api_response, boolean $allow_removing_headers = null ) : Symfony\Component\HttpFoundation\JsonResponse Build a JsonResponse based on an API response object
filterApiResponse ( Elgg\Services\AjaxResponse $api_response, string $hook_type = '' ) : Elgg\Services\AjaxResponse Filter an AjaxResponse through a plugin hook

Method Details

__construct() public method

Constructor
public __construct ( PluginHooksService $hooks, SystemMessagesService $msgs, Input $input, Elgg\Amd\Config $amdConfig )
$hooks Elgg\PluginHooksService Hooks service
$msgs Elgg\SystemMessagesService System messages service
$input Elgg\Http\Input Input service
$amdConfig Elgg\Amd\Config AMD config

appendDeps() public method

Send required AMD modules list back with the response
public appendDeps ( string $hook, string $type, Elgg\Services\AjaxResponse $response, array $params ) : Elgg\Services\AjaxResponse
$hook string "ajax_response"
$type string "all"
$response Elgg\Services\AjaxResponse Ajax response
$params array Hook params
return Elgg\Services\AjaxResponse

appendMessages() public method

Send system messages back with the response
public appendMessages ( string $hook, string $type, Elgg\Services\AjaxResponse $response, array $params ) : Elgg\Services\AjaxResponse
$hook string "ajax_response"
$type string "all"
$response Elgg\Services\AjaxResponse Ajax response
$params array Hook params
return Elgg\Services\AjaxResponse

decodeJson() public method

Attempt to JSON decode the given string
public decodeJson ( mixed $string ) : mixed
$string mixed Output string
return mixed

getViews() public method

Returns an array of views allowed for ajax calls
public getViews ( ) : string[]
return string[]

isAjax2Request() public method

Did the request come from the elgg/Ajax module?
public isAjax2Request ( ) : boolean
return boolean

isReady() public method

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.
public isReady ( ) : boolean
return boolean

registerView() public method

Register a view to be available for ajax calls
public registerView ( string $view ) : void
$view string The view name
return void

respondFromApiResponse() public method

Send a JSON HTTP response based on the given API response
public respondFromApiResponse ( Elgg\Services\AjaxResponse $api_response, string $hook_type = '' ) : Symfony\Component\HttpFoundation\JsonResponse
$api_response Elgg\Services\AjaxResponse API response
$hook_type string The hook type. If given, the response will be filtered by hook
return Symfony\Component\HttpFoundation\JsonResponse

respondFromOutput() public method

Send a JSON HTTP response with the given output
public respondFromOutput ( mixed $output, string $hook_type = '', boolean $try_decode = true ) : Symfony\Component\HttpFoundation\JsonResponse
$output mixed Output from a page/action handler
$hook_type string The hook type. If given, the response will be filtered by hook
$try_decode boolean Try to convert a JSON string back to an abject
return Symfony\Component\HttpFoundation\JsonResponse

respondWithError() public method

Send a JSON HTTP 400 response
public respondWithError ( string $msg = '', integer $status = 400 ) : Symfony\Component\HttpFoundation\JsonResponse
$msg string The error message (not displayed to the user)
$status integer The HTTP status code
return Symfony\Component\HttpFoundation\JsonResponse

unregisterView() public method

Unregister a view for ajax calls
public unregisterView ( string $view ) : void
$view string The view name
return void