soyhuce/laravel-model-injection
Composer 安装命令:
composer require soyhuce/laravel-model-injection
包简介
Extended Model injection for Laravel
README 文档
README
Want to control have better control of model injection ? Need to validate the data before querying the database ?
Here is a package that allows you to do that.
Installation
You can install the package via composer:
composer require soyhuce/laravel-model-injection
Usage
Implicit binding
To validate the url parameter used to inject the model in the controller, you can use
the Soyhuce\ModelInjection\ValidatesImplicitBindings trait in it.
You will have then to implement the method public function routeBindingRules(): array
which will define, for each key on which the model will be bound, the rules to validate the url parameter.
use Soyhuce\ModelInjection\ValidatesImplicitBinding; class Post extends Model { use ValidatesImplicitBinding; /** * @return array<string, mixed> */ public function routeBindingRules(): array { return [ 'id' => 'integer', 'slug' => ['string', 'min:5'] ]; } }
This will allow you to validate the parameter to bind the Post in the routes using:
Route::get('posts/{post}', function(Post $post) { //... }); Route::get('posts-by-slug/{post:slug}', function(Post $post) { //... });
If the parameter is not valid, a 404 error will be returned.
GET /posts/foo => 404
GET /posts-by-slug/bar => 404
See https://laravel.com/docs/routing#implicit-binding
Customize implicit route binding error
You can customize the way this package will handle validation failure for implicit bindings.
In a Service provider, just call InvalidRouteBinding::handleUsing :
InvalidRouteBinding::handleUsing(function (string $class, string $field): never { Log::error("Invalid binding for $class on $field."); abort(422); });
Explicit bindings
You can explicitly bind your models using \Soyhuce\ModelInjection\BindModels trait in a service
provider (RouteServiceProvider for exemple).
use Soyhuce\ModelInjection\BindModels; class RouteServiceProvider extends ServiceProvider { use BindModels; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { parent::boot(); $this->bindModel('user', User::class, 'integer'); // Validates that the parameter is an integer // You can bind a model explicitly on a given column $this->bindModelOn('post', Post::class, ['string', 'min:5'], 'slug'); } }
If the given parameter is not valid, a 404 error will be returned.
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.
soyhuce/laravel-model-injection 适用场景与选型建议
soyhuce/laravel-model-injection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.88k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「soyhuce」 「laravel-model-injection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 soyhuce/laravel-model-injection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 soyhuce/laravel-model-injection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 soyhuce/laravel-model-injection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extra rules for phpstan analysis
Laravel dev-tools
Helpers for Laravel tests
An opinionated JsonResource for Laravel
A set of useful validation rules for Laravel
Write fluent policies in Laravel
统计信息
- 总下载量: 13.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-03