Fork me on GitHub

PHP Callbacks


Jaxon allows the developer to specify global functions that will be called at different steps during the execution of each request in the PHP application.

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.
 *
 * @return bool
 */

The $target parameter 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();
    }

If the callback returns the value true, the request processing is stopped, and the response is sent back to the browser.

After processing the request

$jaxon->callback()->after(function($target, $bEndRequest) {});

The $target parameter is the same as in the before() callback, and $bEndRequest is its return value.

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 before the callback is called.

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 before the callback is called.