承接 dandoetech/laravel-resource-registry 相关项目开发

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

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

dandoetech/laravel-resource-registry

Composer 安装命令:

composer require dandoetech/laravel-resource-registry

包简介

Laravel bridge for DanDoeTech Resource Registry: ClassBasedDriver, capability interfaces, computed resolvers, ServiceProvider.

README 文档

README

Pre-release — Architecture by senior tech lead, implementation largely AI-assisted with human review. Not fully reviewed. Architecture may change before v1.0.0.

Laravel bridge for the DanDoeTech Resource Registry. Auto-discovers Resource classes, binds the Registry as a singleton, and provides capability interfaces for Eloquent integration.

This is the central package — install it first, then add consumer packages as needed.

Installation

composer require dandoetech/laravel-resource-registry

The service provider is auto-discovered. Publish the config:

php artisan vendor:publish --tag=ddt-config

Quick Start

1. Define a Resource

Create a Resource class in app/Resources/:

<?php

declare(strict_types=1);

namespace App\Resources;

use DanDoeTech\ResourceRegistry\Resource;
use DanDoeTech\ResourceRegistry\Builder\ResourceBuilder;
use DanDoeTech\ResourceRegistry\Definition\FieldType;
use DanDoeTech\LaravelResourceRegistry\Contracts\HasEloquentModel;
use DanDoeTech\LaravelResourceRegistry\Contracts\HasPolicy;

class ProductResource extends Resource implements HasEloquentModel, HasPolicy
{
    public function model(): string { return \App\Models\Product::class; }
    public function policy(): string { return \App\Policies\ProductPolicy::class; }

    protected function define(ResourceBuilder $b): void
    {
        $b->key('product')
          ->label('Product')
          ->timestamps()
          ->field('name', FieldType::String, nullable: false, rules: ['required', 'max:120'])
          ->field('price', FieldType::Float, nullable: false, rules: ['required', 'numeric', 'min:0'])
          ->field('category_id', FieldType::Integer, nullable: false)
          ->belongsTo('category', foreignKey: 'category_id')
          ->hasMany('reviews')
          ->computed('category_name', FieldType::String, via: 'category.name')
          ->computed('orders_count', FieldType::Integer, via: 'count:orders')
          ->filterable(['name', 'price', 'category_id', 'category_name'])
          ->sortable(['name', 'price', 'created_at', 'orders_count'])
          ->searchable(['name'])
          ->action('create')
          ->action('update')
          ->action('delete');
    }
}

2. Use the Registry

The Registry is bound as a singleton. Inject or resolve it anywhere:

use DanDoeTech\ResourceRegistry\Registry\Registry;

$registry = app(Registry::class);

$product = $registry->getResource('product');
echo $product->getLabel(); // "Product"

foreach ($registry->all() as $resource) {
    echo $resource->getKey();
}

Resources are auto-discovered from configured paths on first access.

Configuration

config/ddt_registry.php:

return [
    // Directories scanned for Resource classes (relative to base_path)
    'resource_paths' => [
        'app/Resources',
        'Modules/*/Resources',
    ],
];

Note: The API route prefix is configured in config/ddt_api.php (from laravel-generic-api), not here. This config only controls resource discovery.

Glob patterns are supported — Modules/*/Resources discovers resources across all module directories.

Related Packages

This package provides the registry and resource scanning. For a complete setup, you'll also need:

Package Purpose
laravel-generic-api CRUD endpoints (GET/POST/PATCH/DELETE) for all registered resources
laravel-model-meta Generate Eloquent Models + Migrations from registry definitions
laravel-openapi-generator Auto-generate OpenAPI 3.1 spec from registry
laravel-bff Frontend metadata endpoints (fields, forms, tables, permissions)
filament-registry-bridge Generate Filament admin panel from registry definitions

Capability Interfaces

Framework-specific capabilities are opt-in via small interfaces. Implement only what you need:

// Link resource to an Eloquent model
interface HasEloquentModel
{
    /** @return class-string<Model> */
    public function model(): string;
}

// Link resource to a Laravel policy
interface HasPolicy
{
    /** @return class-string */
    public function policy(): string;
}

// Scope queries to the authenticated user's records
interface HasOwnerScope
{
    public function ownerKey(): string;
}

// @deprecated — use HasOwnerScope or Policies instead (removed in v1.0)
interface HasScope
{
    /** @return class-string|Closure|null */
    public function scope(): string|Closure|null;
}

Bridge packages discover capabilities via instanceof:

$modelClass = $resource instanceof HasEloquentModel
    ? $resource->model()
    : throw new \RuntimeException("No model for '{$resource->getKey()}'");

Computed Field Resolvers

For resources with computed fields using via syntax, resolvers are built automatically:

Via syntax Resolver Example
relation.field RelationFieldResolver (subselect) category.name
count:relation RelationCountResolver (withCount) count:orders
pluck:relation.field RelationPluckResolver (GROUP_CONCAT) pluck:tags.name

For custom logic, implement EloquentComputedResolverInterface:

use DanDoeTech\LaravelResourceRegistry\Contracts\EloquentComputedResolverInterface;
use Illuminate\Database\Eloquent\Builder;

class CompletedOrdersCount implements EloquentComputedResolverInterface
{
    public function apply(Builder $query): Builder
    {
        return $query->withCount(['orders as completed_orders' => fn ($q) =>
            $q->where('status', 'completed')
        ]);
    }

    public function filter(Builder $query, mixed $value, string $operator = '='): Builder
    {
        return $query->having('completed_orders', $operator, $value);
    }

    public function sort(Builder $query, string $direction): Builder
    {
        return $query->orderBy('completed_orders', $direction);
    }
}

Reference it in your resource:

->computed('completed_orders', FieldType::Integer, resolver: CompletedOrdersCount::class)

API Overview

Class Purpose
DdtServiceProvider Binds Registry singleton, publishes config
ClassBasedDriver Scans configured paths for Resource classes
PathResolver Resolves glob patterns to PHP file paths
HasEloquentModel Capability: link resource to Eloquent model
HasPolicy Capability: link resource to Laravel policy
HasOwnerScope Capability: scope queries to authenticated user
HasScope (deprecated) Capability: apply query scope
EloquentComputedResolverInterface Contract for custom computed field query logic
ViaResolverFactory Creates resolvers from via syntax strings

Testing

composer install
composer test        # PHPUnit (Orchestra Testbench)
composer qa          # cs:check + phpstan + test

License

MIT

dandoetech/laravel-resource-registry 适用场景与选型建议

dandoetech/laravel-resource-registry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dandoetech/laravel-resource-registry 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15