onursimsek/precondition
Composer 安装命令:
composer require onursimsek/precondition
包简介
HTTP Precondition implementation for Laravel
README 文档
README
HTTP Precondition for Laravel
Installation
To install, run the following command in your project:
composer require onursimsek/precondition
Usage
A client has requested a resource from you (GET), and after a while wants to update the resource and send it back to you (PUT). Meanwhile, another client has received the same resource (GET) and updated it before the first client (PUT). In this case, the first client's update will be based on an incorrect copy and will ignore the second client's updates. This is called lost update problem.
In such a case, you can return 428 Precondition Required to the first client and ask it to pull the resource again.
This package allows you to do this easily. All you need to do is create a Validator class and add it as an attribute to the corresponding controller method.
use Illuminate\Http\Request; use OnurSimsek\Precondition\Validators\PreconditionValidator; class LostUpdateValidator extends PreconditionValidator { public function parameter(Request $request) { return $request->header('If-Unmodified-Since'); } public function __invoke(Request $request): bool { return $this->parameter($request) == $request->route('article')->updated_at; } }
use App\Models\Article; use App\Http\Requests\UpdateArticleRequest; use OnurSimsek\Precondition\Attributes\Precondition; class ArticleController { #[Precondition(LostUpdateValidator::class)] public function update(Article $article, UpdateArticleRequest $request) { $article->fill($request->validated()) $article->save(); return response()->json(); } }
Finally, you need to add precondition middleware to the route you want to use.
Route::put('/articles/{:article}', [ArticleController::class, 'update']) ->middleware(['precondition']);
You can also use this package if you have any conditions for a request to be fulfilled. For example some examples;
Github Sample
You are prompted to re-type the repository name before deleting a repository.
use Illuminate\Http\Request; use OnurSimsek\Precondition\Validators\PreconditionValidator; class DeleteRepositoryValidator extends PreconditionValidator { public function parameter(Request $request) { return $request->input('repository_name'); } public function __invoke(Request $request): bool { return $this->parameter($request) == $request->route('repository')->name; } }
Note: If the repo name is invalid, the response returns with a status code of 412 Precondition Failed.
SMS Verification
SMS verification is a mandatory step in the payment process.
use Illuminate\Http\Request; use OnurSimsek\Precondition\Validators\PreconditionValidator; class SmsValidator extends PreconditionValidator { public function preProcess(): void { cache()->set('sms', '123456'); } public function parameter(Request $request) { return $request->input('sms_code'); } public function __invoke(Request $request): bool { return $this->parameter($request) == cache()->get('sms'); } }
Optional Verification
If you want to validate on a case-by-case basis, you can use the when method as follows.
use Illuminate\Http\Request; use OnurSimsek\Precondition\Validators\PreconditionValidator; class PrivateArticleValidator extends PreconditionValidator { public function when(Request $request) { return $request->route('article')->is_private; } public function parameter(Request $request) { return $request->header('X-Article-Secret-Code'); } public function __invoke(Request $request): bool { return $this->parameter($request) == $request->route('article')->secret_code; } }
You can use the messages() method in the validator class to customize the error messages.
public function messages(): array { return [ 'required' => 'Enter the sms code sent to your phone.', 'failed' => 'Incorrect sms code!', ]; }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
onursimsek/precondition 适用场景与选型建议
onursimsek/precondition 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 44, 最近一次更新时间为 2024 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「request」 「laravel」 「precondition」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 onursimsek/precondition 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 onursimsek/precondition 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 onursimsek/precondition 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides runtime assertions
HTTP request logger middleware for Laravel
Introduces route preconditions when using the Symfony HttpKernel component.
Guard clause assertion library to enforce parameter preconditions
Adds request-parameter validation to the SLIM 3.x PHP framework
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 44
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-03