pareshsoneri/rsp-crud-generator 问题修复 & 功能扩展

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

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

pareshsoneri/rsp-crud-generator

Composer 安装命令:

composer require pareshsoneri/rsp-crud-generator

包简介

A Laravel package to generate API CRUD operations using the Repository-Service Pattern.

README 文档

README

pareshsoneri/rsp-crud-generator is a Laravel package designed to automatically generate CRUD operations for API resources. It adheres to the Repository and Service design patterns to promote clean, maintainable, and scalable code.

With a single command, this package sets up controllers, requests, resources, services, and repositories, enabling efficient management of your application's architecture.You only need to register my provider, as it automatically binds your repository.

Installation

Step 1: Install the Package

Run the following command to install the package via Composer:

composer require pareshsoneri/rsp-crud-generator:dev-main

Step 2: Add the Service Provider

Include the service provider in the providers array of your config/app.php file:

'providers' => [
    // Other service providers...
    RSPCrud\Providers\RepositoryServiceProvider::class,
],

Step 3: Clear Laravel Cache

Ensure all changes are loaded by clearing Laravel's cache:

php artisan optimize:clear

Usage

To generate the CRUD structure for a specific resource, use the following Artisan command:

php artisan make:rsp-crud {Resource}

Replace {Resource} with the name of the resource (e.g., Product).

Example

To create a CRUD structure for the Product resource, run:

php artisan make:rsp-crud Product

Generated Folder Structure

Executing the command generates the following folder and file structure:

/app
    /Http
        /Controllers
            /ProductController.php
        /Requests
            /ProductRequest.php
        /Resources
            /ProductListingResource.php
            /ProductCreateResource.php
            /ProductShowResource.php
    /Services
        /ProductService.php
    /Repositories
        /Contract
            /ProductRepositoryInterface.php
        /Eloquent
            /ProductRepository.php

Magic Behind the Command

Database Schema Inspection

When you run the command php artisan make:rsp-crud User, the package inspects the users table in your database, fetching all columns except id and timestamp fields like created_at and updated_at.

Request Data Generation

The command automatically generates a {Resource}Request.php file containing validation rules for create and update operations. These rules are dynamically created based on the fields in the database table.

Route Generation

The command automatically generates the route and adds it to the api.php file, like this: Route::resource('users', UserController::class);.

Example: UserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users,email',
            'password' => 'required|string|min:8|confirmed',
            // Additional fields dynamically added based on the schema
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'The name field is required.',
            'email.required' => 'The email field is required.',
            'password.required' => 'The password field is required.',
            // Dynamic messages for other fields
        ];
    }
}

API Resource Response Generation

The package generates resource response files that map fields from the database table to API responses:

  • ListingResource: For resource listings with required fields.
  • CreateResource: For responses after resource creation.
  • ShowResource: For detailed resource views.

Example: UserShowResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserShowResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            // Include additional fields from the `users` table
        ];
    }
}

File Breakdown

Controllers

Defines API endpoints for managing CRUD operations.

Requests

Handles validation for incoming data during create and update operations.

Resources

Formats data for API responses:

  • ListingResource: Formats data for resource listings.
  • CreateResource: Formats data after creation.
  • ShowResource: Formats data for detailed resource views.

Services

Encapsulates business logic related to CRUD operations.

Repositories

Manages data access, ensuring separation of concerns:

  • Contract: Defines the repository interface for abstraction.
  • Eloquent: Implements the contract for Eloquent ORM.

Customization

The generated files serve as a starting point. You can extend and modify them to suit your application's unique requirements, such as adding business logic, validation rules, or database relationships.

Features

  • Automated API CRUD Generation: Creates all necessary files for resource management.
  • Repository and Service Pattern: Promotes clean and organized application structure.
  • Flexible and Extendable: Easily adaptable to meet specific project needs.
  • API Focused: Optimized for RESTful API resource management.

Requirements

  • PHP: Version 8.0 or higher.
  • Laravel: Version 9.x or 10.x.

License

This package is open-source and licensed under the MIT License.

Author

Paresh Soneri
Email: soneriparesh435@gmail.com

Contributing

Contributions are welcome! To contribute:

  • Fork the repository.
  • Submit a pull request with your changes.

For bug reports or feature requests, use the GitHub Issues section.

pareshsoneri/rsp-crud-generator 适用场景与选型建议

pareshsoneri/rsp-crud-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 pareshsoneri/rsp-crud-generator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-27