PHPackages                             aolbrich/request-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. aolbrich/request-response

ActiveLibrary[HTTP &amp; Networking](/categories/http)

aolbrich/request-response
=========================

Request and Response

v1.0.6(3y ago)09MITPHPPHP &gt;=8.0.0

Since Apr 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/olbrichattila/php-request-response)[ Packagist](https://packagist.org/packages/aolbrich/request-response)[ RSS](/packages/aolbrich-request-response/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

Request, Response classes with request validator (With Dependency Injection)
============================================================================

[](#request-response-classes-with-request-validator-with-dependency-injection)

Request
=======

[](#request)

### You can create request or response classes individually, or both of them with the following helper:

[](#you-can-create-request-or-response-classes-individually-or-both-of-them-with-the-following-helper)

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);

```

### Create both classes with helper class: (it will be initated as sigleton)

[](#create-both-classes-with-helper-class-it-will-be-initated-as-sigleton)

```
use Aolbrich\RequestResponse\RequestResponse;

[$request, $response] = RequestResponse::initiate();

```

### Create both calsses as non singleton

[](#create-both-calsses-as-non-singleton)

```
use Aolbrich\RequestResponse\RequestResponse;

[$request, $response] = RequestResponse::initiate(false);

```

### Use your existing DI container:

[](#use-your-existing-di-container)

```
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);

```

Note
----

[](#note)

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.

Functions
---------

[](#functions)

```
$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

```

Request validator
-----------------

[](#request-validator)

### It is possible to validate the input with the build in validator: Exampe:

[](#it-is-possible-to-validate-the-input-with-the-build-in-validator-exampe)

```
$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

```

### Example validation errors:

[](#example-validation-errors)

```
Array
(
    [par1] => Required
    [par2] => The value should be more or equal then 5
    [par11] => Date format is incorrect
)

```

### Custom Closure validation:

[](#custom-closure-validation)

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
        },
    ]
);

```

### Add fixed custom closure validator

[](#add-fixed-custom-closure-validator)

```
// 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',
    ]
);

```

### Create your own validation class:

[](#create-your-own-validation-class)

### 1. Create a new class following inheriting the class

[](#1-create-a-new-class-following-inheriting-the-class)

```
