yiisoft / request-model
此包已被废弃且不再维护。未建议替代包。
Yii 框架请求模型
dev-master / 1.0.x-dev
2023-10-10 13:06 UTC
Requires
- php: ^8.0
- psr/container: ^1.0|^2.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- yiisoft/arrays: ^2.0|^3.0
- yiisoft/injector: ^1.0
- yiisoft/middleware-dispatcher: ^5.0
- yiisoft/router: ^3.0
- yiisoft/validator: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- nyholm/psr7: ^1.4
- phpunit/phpunit: ^9.5
- rector/rector: ^0.18.0
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.7
- yiisoft/dummy-provider: ^1.0
- yiisoft/http: ^1.2
- yiisoft/test-support: ^3.0
- yiisoft/translator: ^3.0
This package is auto-updated.
Last update: 2023-10-10 13:08:17 UTC
README
此包已过时。请使用 Yii Input HTTP 代替。
❌
Yii 请求模型
请求模型简化了与请求数据的交互。它允许您对数据进行装饰以方便检索,并在需要时自动验证它。
要求
- PHP 8.0 或更高版本。
安装
可以使用 composer 安装此包
composer require yiisoft/request-model
根据 yiisoft/middleware-dispatcher
文档,您需要通过容器设置 ParametersResolverInterface
的实现为 HandlerParametersResolver
,或直接传递。
一般用法
请求模型的简单版本如下所示
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::post('/test') ->action([SimpleController::class, 'action']) ->name('site/test')
在控制器中使用
use Psr\Http\Message\ResponseInterface; final class SimpleController { public function action(AuthRequest $request): ResponseInterface { echo $request->getLogin(); ... } }
如果数据未通过验证,将抛出 RequestValidationException
。如果您需要处理异常,例如发送响应,可以拦截其中间件。
例如
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()); } } }
您可以使用请求模型而不进行验证。为此,您需要移除 ValidatableModelInterface
。在这种情况下,数据将被包含到模型中,但不会进行验证。例如
final class ViewPostRequest extends RequestModel { public function getId(): int { return (int)$this->getAttributeValue('router.id'); } }
在请求模型类内部,数据可以使用以下键访问
键 | 来源 |
---|---|
查询 | $request->getQueryParams() |
正文 | $request->getParsedBody() |
属性 | $request->getAttributes() |
头 | $request->getHeaders() |
文件 | $request->getUploadedFiles() |
cookie | $request->getCookieParams() |
路由器 | $currentRoute->getArguments() |
数据可以按以下方式获取
$this->requestData['router']['id'];
或通过以下方法
$this->hasAttribute('body.user_id'); $this->getAttributeValue('body.user_id');
属性
您可以在动作处理器中使用属性从请求中获取数据
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; //... } }
属性也支持闭包动作。
有一些内置属性
名称 | 源代码 |
---|---|
正文 | 请求的解析正文 |
查询 | URI 的查询参数 |
请求 | 请求属性 |
路由 | 当前路由的参数 |
上传文件 | 请求的上传文件 |
单元测试
此包使用 PHPUnit 进行测试。要运行测试
./vendor/bin/phpunit
突变测试
此包的测试使用 Infection 突变框架进行验证。要运行它
./vendor/bin/infection
静态分析
代码使用 Psalm 进行静态分析。要运行静态分析
./vendor/bin/psalm
支持项目
关注更新
许可
Yii 请求模型是自由软件。它根据 BSD 许可证发布。有关更多信息,请参阅 LICENSE
。
由 Yii Software 维护。