承接 yiisoft/request-model 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yiisoft/request-model

Composer 安装命令:

composer require yiisoft/request-model

包简介

Yii Framework Request Model

README 文档

README

This package is deprecated. Use Yii Input HTTP instead.

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Request Model


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

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

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

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.

yiisoft/request-model 适用场景与选型建议

yiisoft/request-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.95k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2020 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validation」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yiisoft/request-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yiisoft/request-model 我们能提供哪些服务?
定制开发 / 二次开发

基于 yiisoft/request-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 42.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 19
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 15
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-10-18