定制 ebsp/resting 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ebsp/resting

Composer 安装命令:

composer require ebsp/resting

包简介

Typed REST resources, validation, and OpenAPI generation for Laravel.

README 文档

README

Typed REST resources, validation, and OpenAPI generation for Laravel.

Resting is an alternative to Laravel's built-in API Resources, built around typed field objects. Each resource property is a strongly-typed Field that knows how to parse, validate, and format itself — so request payloads, validation rules, response shapes, and OpenAPI schemas all derive from a single source of truth.

class UserResource extends \Seier\Resting\Resource
{
    public StringField $name;
    public IntField $age;

    public function __construct()
    {
        $this->name = new StringField();
        $this->age = new IntField();
    }
}

Why Resting?

  • One definition, many uses. A resource serves as the request schema, the validator, the response shape, and the OpenAPI component. No duplicated FormRequest + Resource + Swagger annotations.
  • Strongly typed fields. Every field knows its own type, parser, and validators — no string-keyed validation rules to keep in sync with response transformers.
  • Composable validation. Per-field validators, predicate-based conditional validation (whenProvided, whenEquals, …), and resource-level cross-field comparisons (lessThan, equal, …).
  • Polymorphic resources. UnionResource for discriminator-tagged unions, DynamicResource for runtime field shapes.
  • OpenAPI 3 out of the box. Generate a spec from your route collection with no extra annotations.

Requirements

  • PHP 8.2+
  • Laravel 11 or 12

Installation

composer require ebsp/resting

The service provider is auto-registered. Optionally publish the config:

php artisan vendor:publish --tag=config --provider="Seier\Resting\Support\Laravel\RestingServiceProvider"

Quickstart

Define a resource

use Seier\Resting\Resource;
use Seier\Resting\Fields\IntField;
use Seier\Resting\Fields\StringField;

class UserResource extends Resource
{
    public StringField $name;
    public IntField $age;

    public function __construct()
    {
        $this->name = (new StringField())->trim();
        $this->age = (new IntField())->required();
    }
}

Validate and parse a request

Route::post('/users', function (Request $request) {
    $user = (new UserResource())->set($request->all());

    // Persist…
    return $user;
});

Resource implements Laravel's Jsonable, so returning the resource is enough — Laravel encodes it as JSON automatically. Invalid input throws Illuminate\Validation\ValidationException with nested error paths.

Build a response

return (new UserResource())->set($model->toArray());

Or map a collection:

return (new UserResource())->mapMany(
    $users,
    fn (UserResource $r, User $u) => $r->set($u->toArray()),
);

Field types

Field Description
StringField Strings, with trim(), upper(), lower(), stripWhitespace(), transform() transformers
IntField Integers, with numeric validators (min, max, between, …)
NumberField Floats / decimals
BoolField Booleans (parses true/false, 1/0, yes/no)
EnumField PHP backed enums (string or int)
CarbonField Date-times (returns CarbonImmutable)
CarbonPeriodField Date ranges as CarbonPeriod
TimeField Time-of-day without date
ArrayField Homogeneous arrays with element-type validation
ResourceField Single nested resource
ResourceArrayField Array of nested resources
RawField Pass-through, no parsing or validation

All fields support required(), nullable(), forbidden(), and omittedDefault().

Conditional validation

Predicate factories let you express rules like "shippingAddress is required when requiresShipping is true":

use function Seier\Resting\Validation\Predicates\whenEquals;

public function __construct()
{
    $this->requiresShipping = new BoolField();
    $this->shippingAddress = (new StringField())
        ->required(whenEquals($this->requiresShipping, true));
}

Polymorphic resources

UnionResource discriminates between resource shapes by a tag field:

class AnimalResource extends UnionResource
{
    public function __construct()
    {
        parent::__construct('type', fn () => [
            'cat' => new CatResource(),
            'dog' => new DogResource(),
        ]);
    }
}

DynamicResource lets you build resources at runtime:

$resource = (new DynamicResource())
    ->withField('name', new StringField())
    ->withField('count', new IntField());

OpenAPI generation

Resting can produce an OpenAPI 3 document from your registered routes:

use Seier\Resting\Support\OpenAPI;

$spec = (new OpenAPI(Route::getRoutes()))->toArray();

Annotate routes with helper macros:

Route::post('/users', UserController::class)
    ->docs('Create a new user.')
    ->lists(UserResource::class);

Resources, query parameters, and route metadata are turned into components.schemas, components.parameters, and paths automatically.

Documentation

Full documentation, including all field types, validation predicates, the marshaller, and OpenAPI generation, is available at ebsp.github.io/resting (coming soon).

Testing

composer install
vendor/bin/phpunit

Contributing

Issues and pull requests are welcome. Please run the test suite before submitting a PR.

License

Resting is open-sourced software licensed under the MIT license.

ebsp/resting 适用场景与选型建议

ebsp/resting 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.04k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「validation」 「laravel」 「transformer」 「resources」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ebsp/resting 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ebsp/resting 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-11