Stages

December 2, 2025 ยท View on GitHub

Stages of handling a request in restler

When restler receives a request it handles it in the following stages:

  1. get: Restler identifies request method, url, data and format
  2. route: Finding out a matching route for the request
  3. negotiate Applies content negotiation as requested by the api client.
  4. preAuthFilter: If there is a filer applicable to the route before authentication it is applied.
  5. Authenticate: If the selected route requires authentication it is applied.
  6. postAuthFilter: If there is a filer applicable to the route after successful authentication it is applied.
  7. validate: Applies validation for each and every parameter as defined through the phpdoc comments.
  8. call: Finally calls the php method with the validated parameters.
  9. message: Return value of the above call sent as the response in the selected format.

A successful request goes through all the above stages. Whereas a failed request runs until the stage of the failure and then returns a composed failure message taking the fail fast approach for improved performance.

We can subscribe to any of the following events to listen to them.

methodsignaturedescription
onGetonGet(Callable $function)fired before reading the request details
onRouteonRoute(Callable $function)fired before finding the api method
onNegotiateonNegotiate(Callable $function)fired before content negotiation
onPreAuthFilteronPreAuthFilter(Callable $function)fired before pre auth filtering
onAuthenticateonAuthenticate(Callable $function)fired before auth
onPostAuthFilteronPostAuthFilter(Callable $function)fired before post auth filtering onValidate()
onValidateonValidate(Callable $function)fired before validation
onCallonCall(Callable $function)fired before api method call
onComposeonCompose(Callable $function)fired before composing response
onRespondonRespond(Callable $function)fired before sending response
onCompleteonComplete(Callable $function)fired after sending response
onMessageonMessage(Callable $function)fired before composing error response

These methods are available both statically and dynamically at runtime. so both the following examples are valid.

Usage Examples:

1. Logging successful api responses

We can use onComplete method for logging.

require_once '../vendor/autoload.php';

use Luracast\Restler\Restler;
use Luracast\Restler\User;

$r = new Restler();

$r->onComplete(function () use ($r) {
    $log = array(
        'api'     =>json_encode($r->apiMethodInfo->parameters),
        'ip'      => User::getIpAddress(),
        'route'   => $r->apiMethodInfo->className.'::'.$r->apiMethodInfo->methodName,
        'method'  => $r->requestMethod,
        'parameters' => json_encode($r->apiMethodInfo->parameters)
    );
    print_r($log); //your logging function here!
});

use Luracast\Restler\Routes;

Routes::mapApiClasses([Say::class]);
$r->handle();

2. Lazily setting form style.

require_once '../vendor/autoload.php';

use Luracast\Restler\Restler;
use Luracast\Restler\UI\Forms;
use Luracast\Restler\UI\Foundation5Form;

//no dependency on restler instance so it can be called statically
Restler::onCall(function () { 
    Forms::setStyles(new Foundation5Form());
});

use Luracast\Restler\Restler;
use Luracast\Restler\Routes;

Routes::mapApiClasses([Something::class]);
(new Restler)->handle();