PHP code example of aolbrich / request-response

1. Go to this page and download the library: Download aolbrich/request-response 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/ */

    

aolbrich / request-response example snippets


$validated = $request->validate(
    [
        'email' => 'tNumber' => 'regex:/^[0-9]+$/',
    ]
);

print_r($validated); // The fields validated,
print_r($request->validationErrors()); // The fields errored with the error description

$validated = $request->validate(
    [
        'email' => 't' => function (mixed $accountId) {
            $accountExist = $this->accountExists($accountId)
            if ($accountExist) {
                return "Account {$accountId} already exists";    
            }

            return null
        },
    ]
);

// Must preceed the validation
$request->setRule('customRule', function (mixed $value) {
    return "If {$value} does not validate, then return error message, if validate return null";
});

$validated = $request->validate(
    [
        'accountNumber' => 'customRule',
    ]
);


$response->setResponseCode(404);

$response->getResponseCode();

$response->setHeader('Content-type', "application/json");

$response->headers();

$response->getBody();

$response->render();

$response->setResponseCode(404);
$response->setBody('404 Not Found');
$response->render();

$response->setHeader('Content-type', "application/json");
$response->setBody(json_encode(['message' => 'Hello World']));
$response->render();

## JSON Response
### You can alternatively render JSON responses