Callbacks
- Version 4.x
- Version 3.x
- Version 2.x
Jaxon allows the developer to specify callbacks that will be called at different steps during the execution of each request.
Starting from version 3.6.0, multiple functions can be defined for the same step.
When booting the library
$jaxon->callback()->boot(function() {});
This callback is available since version 3.6.0. The provided function takes no parameter.
At a Jaxon class instantiation
$jaxon->callback()->init(function($xCallable) {});
This callback is called for each class which is instantiated when processing a request. The provided function takes the newly created instance as parameter.
Before processing the request
$jaxon->callback()->before(function($target, &$bEndRequest) {});
The provided callback must accept the following parameters.
/**
* @param Jaxon\Request\Target $target The function or class method to be called.
* @param boolean &$bEndRequest Set this to true to end the request.
*
* @return Jaxon\Response\Response
*/
The parameter $target
allows to retrieve the called function or class, as follows.
if($target->isFunction())
{
$function = $target->getFunctionName();
}
elseif($target->isClass())
{
$class = $target->getClassName();
$method = $target->getMethodName();
}
The boolean parameter $bEndRequest
is passed by reference. Its initial value is false
, and if it is assigned the value true
in the callback, the request processing is stopped, and the returned response is sent back to the browser.
After processing the request
$jaxon->callback()->after(function($target, $bEndRequest) {});
The parameters are the same as in the before()
callback, except that $bEndRequest
is passed by value and not by reference.
If the callback returns a Jaxon response, it is then appended to the current response.
In the case of an invalid request
$jaxon->callback()->invalid(function($sMessage) {});
The callback parameter is the error message returned when processing the request. The response to the Jaxon request is reset, and if the callback returns one, it will also be that of the request.
In the case of a processing error
$jaxon->callback()->error(function($xException) {});
The parameter is the exception thrown while processing the request. The response to the Jaxon request is reset, and if the callback returns one, it will also be that of the request.