binarcode/laravel-restable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

binarcode/laravel-restable

Composer 安装命令:

composer require binarcode/laravel-restable

包简介

Lightweight Laravel API.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 binarcode/laravel-restable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-03