承接 benyaminrmb/laravel-dynamic-resources 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

benyaminrmb/laravel-dynamic-resources

Composer 安装命令:

composer require benyaminrmb/laravel-dynamic-resources

包简介

Dynamic API resources with mode-based field selection for Laravel. Define minimal, default, detailed modes and compose them on the fly.

README 文档

README

Latest Version on Packagist Total Downloads Tests PHPStan

Dynamic API resources with mode-based field selection for Laravel. Define minimal, default, detailed modes and compose them on the fly. Reduce API payload sizes, improve performance, and give clients control over response structure.

Features

  • Mode-based field selection - Define minimal, default, detailed modes and switch between them
  • Composable modes - Combine modes with ->withAvatar()->withPosts()
  • Field filtering - Fine-grained control with only() and except()
  • Nested resource support - Mode inheritance through resource trees
  • Request-based selection - Let clients choose modes via query params: ?mode=minimal&fields=id,name
  • PHP Attributes - Modern declarative syntax with #[Field] and #[Mode] attributes
  • Artisan commands - make:dynamic-resource and resource:list
  • Strict type safety - PHP 8.4, PHPStan level 9

Requirements

  • PHP 8.4+
  • Laravel 11.0+ or 12.0+

Installation

composer require benyaminrmb/laravel-dynamic-resources

Publish the config (optional):

php artisan vendor:publish --tag=dynamic-resources-config

Quick Start

1. Create a Resource

php artisan make:dynamic-resource UserResource --model=User

Or create manually:

<?php

namespace App\Http\Resources;

use Benyaminrmb\LaravelDynamicResources\DynamicResource;

class UserResource extends DynamicResource
{
    public function fields(): array
    {
        return [
            // Minimal mode - just IDs and names
            'minimal' => [
                'id',
                'name',
            ],

            // Default mode - standard fields
            'default' => [
                'id',
                'name',
                'email',
            ],

            // Detailed mode - inherits default + adds more
            'detailed' => fn() => [
                ...$this->mode('default'),
                'bio' => $this->bio,
                'created_at' => $this->created_at->toISOString(),
                'posts' => PostResource::collection($this->whenLoaded('posts')),
            ],

            // Composable modes - add these with withAvatar(), withPosts()
            'avatar' => fn() => [
                'avatar_url' => $this->avatar_url,
            ],

            'posts' => fn() => [
                'posts' => PostResource::collection($this->whenLoaded('posts')),
            ],
        ];
    }
}

2. Use in Controllers

class UserController extends Controller
{
    // Single resource with mode
    public function show(User $user)
    {
        return UserResource::make($user)->detailed();
    }

    // Collection with mode
    public function index()
    {
        return UserResource::collection(User::all())->minimal();
    }

    // Compose multiple modes
    public function profile(User $user)
    {
        return UserResource::make($user)
            ->minimal()
            ->withAvatar()
            ->withPosts();
    }

    // Field filtering
    public function summary(User $user)
    {
        return UserResource::make($user)
            ->default()
            ->only(['id', 'name', 'email']);
    }
}

Mode Composition

Modes are composable. You can combine them dynamically:

// Start with minimal, add avatar and posts
UserResource::make($user)
    ->minimal()
    ->withAvatar()
    ->withPosts();

// Remove a mode
UserResource::make($user)
    ->detailed()
    ->withoutPosts();

// Chain as many as you need
UserResource::make($user)
    ->minimal()
    ->withAvatar()
    ->withTimestamps()
    ->withoutTimestamps()  // Changed our mind
    ->withPosts();

Field Filtering

Fine-grained control over which fields appear:

// Include only these fields
UserResource::make($user)
    ->default()
    ->only(['id', 'name']);

// Exclude these fields
UserResource::make($user)
    ->detailed()
    ->except(['created_at', 'updated_at']);

Mode Inheritance

Reference other modes to build upon them:

public function fields(): array
{
    return [
        'minimal' => ['id', 'name'],

        'default' => fn() => [
            ...$this->mode('minimal'),  // Include all minimal fields
            'email',
        ],

        'detailed' => fn() => [
            ...$this->mode('default'),  // Include all default fields
            'bio',
            'created_at',
        ],
    ];
}

Conditional Fields

Use Laravel's conditional helpers:

public function fields(): array
{
    return [
        'default' => fn() => [
            'id',
            'name',
            'avatar' => $this->when($this->avatar !== null, $this->avatar),
            'posts_count' => $this->whenCounted('posts'),
            'posts' => $this->whenLoaded('posts', fn() => PostResource::collection($this->posts)),
        ],
    ];
}

Collections

Collections inherit modes from the parent:

// All users will use minimal mode
UserResource::collection($users)->minimal();

// Compose modes on collections too
UserResource::collection($users)
    ->minimal()
    ->withAvatar();

// Field filtering on collections
UserResource::collection($users)
    ->default()
    ->only(['id', 'name']);

Request-Based Field Selection

Enable the middleware to let clients control response structure:

// routes/api.php
Route::middleware('resource.fields')->group(function () {
    Route::get('/users', [UserController::class, 'index']);
});

Now clients can use:

GET /api/users?mode=minimal
GET /api/users?fields=id,name,email
GET /api/users?mode=detailed&exclude=created_at

Additional Data

Add metadata to responses:

UserResource::make($user)
    ->minimal()
    ->additional([
        'meta' => [
            'version' => '2.0',
            'generated_at' => now()->toISOString(),
        ],
    ]);

Artisan Commands

# Create a new dynamic resource
php artisan make:dynamic-resource UserResource --model=User

# List all dynamic resources and their modes
php artisan resource:list --modes

Configuration

Publish and customize:

php artisan vendor:publish --tag=dynamic-resources-config

Key options in config/dynamic-resources.php:

return [
    // Default mode when none specified
    'default_mode' => 'default',

    // Throw exceptions for undefined modes
    'strict_mode' => env('DYNAMIC_RESOURCES_STRICT', false),

    // Request-based field selection
    'request_selection' => [
        'enabled' => true,
        'mode_parameter' => 'mode',
        'fields_parameter' => 'fields',
        'forbidden_modes' => ['admin', 'internal'],
    ],
];

Migration from v1

If upgrading from v1, the main changes are:

  1. Class renamed: ModularResourceDynamicResource
  2. Service provider renamed: LaravelModularResourcesServiceProviderDynamicResourceServiceProvider
  3. PHP 8.4 required (was 8.2)
// v1
use Benyaminrmb\LaravelDynamicResources\ModularResource;

// v2
use Benyaminrmb\LaravelDynamicResources\DynamicResource;

Testing

composer test

Static Analysis

composer analyse

Code Style

composer format

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email benyaminrmb@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

benyaminrmb/laravel-dynamic-resources 适用场景与选型建议

benyaminrmb/laravel-dynamic-resources 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 benyaminrmb/laravel-dynamic-resources 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-14