binarcode/laravel-restable
Composer 安装命令:
composer require binarcode/laravel-restable
包简介
Lightweight Laravel API.
README 文档
README
Installation
You can install the package via composer:
composer require binarcode/laravel-restable
Prerequisite
To associate search with a model, the model must implement the following interface and trait:
namespace App\Models; use BinarCode\LaravelRestable\HasRestable; use BinarCode\LaravelRestable\Restable; use Illuminate\Database\Eloquent\Model; class YourModel extends Model implements Restable { use HasRestable; }
Usage
Using this lightweight package you can mainly customize search, matches and sorts.
Search
Define columns you want to be searchable using $search model property:
class Dream extends Model implements Restable { use HasRestable; public static array $search = [ 'name', ]; }
Now in the query of your request you can use the ?search= to find over your model. Let's assume this is the URL for
geting the list of dreams:
GET: /api/dreams?search=be happy
Then in the controller you may have something like this:
use App\Models\Dream; use Illuminate\Http\Request; class DreamController extends Controller { public function index(Request $request) { $dreams = Dream::search($request); return response()->json($dreams->paginate()); } }
This way Restable will find your dreams by name column and will return a Builder instance, so you can paginate
or do whatever you want over that query.
The query filtering is something like this: $query->where('column_name', 'like', "%$value%";
Match
Matching by a specific column is a more strict type of search. You should define the columns you want to match along with the type:
use BinarCode\LaravelRestable\Types; class Dream extends Model implements Restable { use HasRestable; public static array $match = [ 'id' => Types::MATCH_ARRAY, 'name' => Types::MATCH_TEXT, ]; }
The URL may look like this:
GET: /api/dreams?id=1,2,3&name=happy
So Restable will make a query like this:
$query->whereIn('id', [1, 2, 3])->where('name','=', 'happy');
The controller could be same as we had for the search.
Sort
You can also specify what columns could be sortable:
class Dream extends Model implements Restable { use HasRestable; public static array $sort = [ 'id', 'name', ]; }
The query params for sort could indicate whatever is asc or desc sorting by using the - sign:
Sorting desc by id column:
GET: /api/dreams?sort=-id
Sorting asc by id column:
GET: /api/dreams?sort=id
Customizations
Model Methods
You can use methods to return your search, matches or sorts from the model definition:
class Dream extends Model implements Restable { use HasRestable; public static function sorts(): array { return [ 'id', 'name' ]; } public static function matches(): array { return [ 'id' => 'int', 'name' => 'string', ]; } public static function searchables(): array { return ['name']; } }
Custom filters
Instead of using the default methods for filtering, you can have your own:
use BinarCode\LaravelRestable\Filters\MatchFilter;class Dream extends Model implements Restable { use HasRestable; public static function matches(): array { return [ 'something' => MatchFilter::make()->resolveUsing(function($request, $query) { // filter it here }), 'name' => 'string' ]; } }
So you can now match by something property, and implement your own search into the closure.
You can also create your own Match filter class, and implement the search there:
use BinarCode\LaravelRestable\Filters\MatchFilter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; class MatchSomething extends MatchFilter { public function apply(Request $request, Builder $query, $value): Builder { // your filters return $query->where('a', $value); } }
Then use your MatchSomething filter:
class Dream extends Model implements Restable { use HasRestable; public static function matches(): array { return [ 'something' => MatchSomething::make(), 'name' => 'string' ]; } }
The same you could do for Search or Sort filter, by extending the BinarCode\LaravelRestable\Filters\SearchableFilter or BinarCode\LaravelRestable\Filters\SortableFilter filters.
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.
binarcode/laravel-restable 适用场景与选型建议
binarcode/laravel-restable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「binarcode」 「laravel-restable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 binarcode/laravel-restable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 binarcode/laravel-restable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 binarcode/laravel-restable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lightweight package to log slack exceptions.
Laravel email scheduler
A Laravel wrapper for the Segment API and events tracking.
Just another multi tenant support package for Laravel.
A realtime chat implementation for laravel nova.
Alfabank REST API integration
统计信息
- 总下载量: 1.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-03