PHP code example of laurynasgadl / restponder-php

1. Go to this page and download the library: Download laurynasgadl/restponder-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laurynasgadl / restponder-php example snippets


use Luur\Restponder\Restponder;

$response = Restponder::content('happy little 🌳');

use Luur\Restponder\Restponder;

$response = Restponder::content('happy little 🌳');
$response->addMetadata('request_id', '1234-5678');

use Luur\Restponder\Restponder;

$response = Restponder::content(new Exception('Oops', 987));

use Luur\Restponder\Restponder;

Restponder::setErrorIncludeDebug(true);
$response = Restponder::content(new Exception('Oops', 987));

use Luur\Restponder\ErrorData;
use Luur\Restponder\Restponder;

$handler = function (Exception $exception, ErrorData $data) {
    $data->addDetail('is_validation_exception', $exception instanceof ValidationException);
};

Restponder::registerErrorHandler(Exception::class, $handler);
$response = Restponder::content(new Exception('Oops', 987));

use Luur\Restponder\ResponseContent;
use Luur\Restponder\Restponder;

$handler = function (Exception $exception, ResponseContent $response) {
    $response->addMetadata('failed', true);
    $response->addMetadata('error_message', $exception->getMessage());
};

Restponder::registerResponseHandler(Exception::class, $handler);

use Luur\Restponder\ErrorData;
use Luur\Restponder\Restponder;

$handler = function (Exception $exception, ErrorData $data) {
    $data->setMessage($exception->getCode().$exception->getMessage());
    $data->addDetail('test', true);
};

Restponder::registerErrorHandler(Exception::class, $handler);