codezero/laravel-route-key-exists
Composer 安装命令:
composer require codezero/laravel-route-key-exists
包简介
Laravel validation rule to check if a custom route key exists.
README 文档
README
Laravel validation rule to check if a custom route key exists.
Laravel's exists rule checks a database table for a column with a given value. This validation rule uses the resolveRouteBinding() method on a model to check if a given value exists.
Requirements
- PHP >= 7.0
- Laravel >= 5.5
Installation
Require the package via Composer:
composer require codezero/laravel-route-key-exists
Some Background Info
Laravel's implicit route model binding allows you to automatically resolve a model by type hinting it in a controller. Furthermore, you can change the route key that is used to query the database in your model:
public function getRouteKeyName() { return 'id'; }
This also works when you are using a custom or computed route key in your model:
public function getRouteKey() { // "encode" the route key return "foo-{$this->id}"; } public function resolveRouteBinding($value) { // "decode" the route key $id = (int) str_replace('foo-', '', $value); // resolve from the database return $this->where('id', $id)->first(); }
But what if you are sending a custom key in a POST request and you want to validate it? Unlike Laravel's exists rule, this validation rule uses the resolveRouteBinding() method to check if the key is valid.
Usage
Let's say you have a model with an ID of 1, but getRouteKey() returns the encoded value of 1234.
In your validation rules, pass your model's class name to \CodeZero\RouteKeyExists\RouteKeyExists:
request()->validate([ 'model_id' => RouteKeyExists::model(Model::class), ]);
Here, model_id is the encoded value, which will be resolved using the resolveRouteBinding() method on your model. If it can't be resolved, validation fails.
Possibly, you will need the actual ID to work with when validation passes. Tack on replace() to the rule and model_id will be updated to the actual ID:
request()->validate([ 'model_id' => RouteKeyExists::model(Model::class)->replace(), ]); $id = request('model_id'); // actual ID
If your form uses a different attribute name than your model or database, you can replace the ID and the attribute name in the process.
request()->validate([ 'model' => RouteKeyExists::model(Model::class)->replace('model_id'), ]); $id = request('model_id'); // actual ID //$id = request('model'); // null
Or maybe you want to keep the encoded ID in the request, but add the actual ID as well. Just tack on add() and specify an attribute name:
request()->validate([ 'model_id' => RouteKeyExists::model(Model::class)->add('actual_id'), ]); $id = request('actual_id'); // actual ID $key = request('model_id'); // route key
Beware that attributes that are dynamically added to the request will not be included in the array that is returned from request()->validate(). You can access those via request('attribute_name').
Useful Packages
- This rule works perfectly with the
laravel-optimusmodel trait.
Testing
vendor/bin/phpunit
Security
If you discover any security related issues, please e-mail me instead of using the issue tracker.
Changelog
See a list of important changes in the changelog.
License
The MIT License (MIT). Please see License File for more information.
codezero/laravel-route-key-exists 适用场景与选型建议
codezero/laravel-route-key-exists 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 149 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「model」 「id」 「laravel」 「key」 「binding」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codezero/laravel-route-key-exists 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codezero/laravel-route-key-exists 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codezero/laravel-route-key-exists 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
Interfaces for web resource models and services to retrieve and create them
A Laravel-like router for the WordPress Rewrite API
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 149
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-25