Note, if you initiate individually, you have to use the dependency injection to initiate the class(es)
use Aolbrich\PhpDiContainer\Container;
use Aolbrich\RequestResponse\Http\Request\Request;
use Aolbrich\RequestResponse\Http\Response\ResponseInterface;
$container = new Container();
// Initiate
$request = $container->get(Request::class);
$response = $container->get(Response::class);
// Initiate as singleton
$request = $container->singleton(Request::class);
$response = $container->singleton(Response::class);
use Aolbrich\RequestResponse\RequestResponse;
[$request, $response] = RequestResponse::initiate();
use Aolbrich\RequestResponse\RequestResponse;
[$request, $response] = RequestResponse::initiate(false);
use Aolbrich\PhpDiContainer\Container;
use Aolbrich\RequestResponse\RequestResponse;
$container = new Container();
// set first parameter to false if the classes should not be singleton
[$request, $response] = RequestResponse::initiate(true, $container);
Singleton here means that if you re-create the class with the DI container like $container->get(Request::class);
then it will return with the same request or response object.
$request->getUri(); // Returns the URI
$request->getMethod(); // Returns the method like GET, POST, DELETE...
$request->body(); // Returns the POST request body
$request->jsonBody(); // If the body is json, it will return the array representation of the json, otherwise null
$request->params(); // Return the GET and POST parameters sanitized for displaying, storing
$validated = $request->validate(
[
'email' => 'required',
'age' => 'required|min:18|max:65',
'accountNumber' => 'regex:/^[0-9]+$/',
]
);
print_r($validated); // The fields validated,
print_r($request->validationErrors()); // The fields errored with the error description
Array
(
[par1] => Required
[par2] => The value should be more or equal then 5
[par11] => Date format is incorrect
)
If the closure returns null then it will be valid, otherwise return an error message.
$validated = $request->validate(
[
'email' => 'required',
'age' => 'required|min:18|max:65',
'account' => 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',
]
);
<?php
declare(strict_types=1);
namespace <your namespace>;
use Aolbrich\RequestResponse\Http\Request\Validator\ValidationRuleInterface;
use Aolbrich\RequestResponse\Http\Request\Validator\Rules\ValidationRuleBase;
class YourValidationRule extends ValidationRuleBase implements ValidationRuleInterface
{
public function applyRule(mixed $value, string $validationParam = ''): bool
{
return (bool) strtotime($value);
}
public function message(): string
{
return 'Date format is incorrect';
}
}
$request->setRule('customRule', <your namespace>\YourValidationRule::class);
The response object renders a text (or using other class json) with HTTP headers and status code
Set response code
$response->setResponseCode(404);
Get response code
$response->getResponseCode();
Set headers
$response->setHeader('Content-type', "application/json");
Get the headers
$response->headers();
Set text (body)
$response->setBody('Body text');
Get body test
$response->getBody();
Render the response
$response->render();
Example: render 404
$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
Example:
use Aolbrich\RequestResponse\Http\Response\JsonResponse;
$jsonResponse = new JsonResponse();
$jsonResponse->arrayToJson(['message' => 'Hello World']);
$jsonResponse->render();
The class has additional functions next to the base response class:
arrayToJson(<array>); // This will be returned as a json when calling render
getBodyAsArray(); // This will return an array or null, what is previously set
mergeToJson(<array>) Merges the existing content with a new array
- CORRS, Header support
- Further validators