Get support for yiisoft/request-model
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
yiisoft/request-model
Request model simplifies working with request data. It allows you to decorate data for easy retrieval and automatically validate it when needed.
Requirements
- PHP 8.0 or higher.
Installation
The package could be installed with composer:
composer require yiisoft/request-model
According to yiisoft/middleware-dispatcher
docs, you need to set
the implementation of ParametersResolverInterface
to HandlerParametersResolver
via container or pass directly.
General usage
A simple version of the request model looks like the following:
use Yiisoft\RequestModel\RequestModel;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;
final class AuthRequest extends RequestModel implements RulesProviderInterface
{
public function getLogin(): string
{
return (string)$this->getAttributeValue('body.login');
}
public function getPassword(): string
{
return (string)$this->getAttributeValue('body.password');
}
public function getRules(): array
{
return [
'body.login' => [
new Required(),
new Email(),
],
'body.password' => [
new Required(),
]
];
}
}
Route:
Route::post('/test')
->action([SimpleController::class, 'action'])
->name('site/test')
Usage in controller:
use Psr\Http\Message\ResponseInterface;
final class SimpleController
{
public function action(AuthRequest $request): ResponseInterface
{
echo $request->getLogin();
...
}
}
If the data does not pass validation, RequestValidationException
will be thrown.
If you need to handle an exception and, for example, send a response, you can intercept its middleware.
For example:
final class ExceptionMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (RequestValidationException $e) {
return new Response(400, [], $e->getFirstError());
}
}
}
You can use the request model without validation. To do this, you need to remove the ValidatableModelInterface
.
In this case, the data will be included into the model, but will not be validated. For example:
final class ViewPostRequest extends RequestModel
{
public function getId(): int
{
return (int)$this->getAttributeValue('router.id');
}
}
Inside the request model class, data is available using the following keys:
key | source |
---|---|
query | $request->getQueryParams() |
body | $request->getParsedBody() |
attributes | $request->getAttributes() |
headers | $request->getHeaders() |
files | $request->getUploadedFiles() |
cookie | $request->getCookieParams() |
router | $currentRoute->getArguments() |
This data can be obtained as follows
$this->requestData['router']['id'];
or through the methods
$this->hasAttribute('body.user_id');
$this->getAttributeValue('body.user_id');
Attributes
You can use attributes in an action handler to get data from a request:
use Psr\Http\Message\ResponseInterface;
use Yiisoft\RequestModel\Attribute\Request;
use Yiisoft\RequestModel\Attribute\Route;
final class SimpleController
{
public function action(#[Route('id')] int $id, #[Request('foo')] $attribute,): ResponseInterface
{
echo $id;
//...
}
}
Attributes are also supported in closure actions.
There are several attributes out of the box:
Name | Source |
---|---|
Body | Parsed body of request |
Query | Query parameter of URI |
Request | Attribute of request |
Route | Argument of current route |
UploadedFiles | Uploaded files of request |
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/infection
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
Support the project
Follow updates
License
The Yii Request Model is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2