定制 oscabrera/model-repository 二次开发

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

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

oscabrera/model-repository

Composer 安装命令:

composer require oscabrera/model-repository

包简介

A Laravel package that streamlines the creation of repository-based structures with a custom Artisan command.

README 文档

README

Latest Version on Packagist Total Downloads

VitePress PHPStan Pint PHPMD

built with Codeium

model-repository

Please follow the documentation at model-repository.

This package for Laravel greatly simplifies the process of creating a complete RESTful API for any model in your application. By just running a command, you can generate all the files necessary to create, read, update and delete instances of said model. This project is based on the Repository pattern, separating data access logic from business logic, resulting in cleaner, more modular and easier to maintain code. In addition, it integrates services that manage the business logic and serve to orchestrate operations on the repository, thus providing an additional layer of abstraction and organization to the code.

Main features:

  • Automated file generation: When you run the php artisan make:repository command, the following files will be automatically generated:
    • Model (Model): The Eloquent model that defines the database table structure.
    • Repository to manage the model (Repository): Implements the repository pattern to encapsulate database access logic.
  • Option to Automated file generation
    • Service for the repository (Service): Implements the business logic for the model.
    • Controller for the service (Controller): Defines API routes and uses the service for API logic.
    • Validation requests (Request): Validates input data for controller routes.
    • Seeder (Seeder): Populates the database with sample data for testing purposes.
    • Factory (Factory): Generates dummy data for testing purposes.
  • Predefined CRUD methods: The generated Repository, Service and Controller implement the standard methods of a RESTful API:
    • create: Create a new instance of the model
    • read: Read a specific instance of the model
    • update: Update an existing instance of the model
    • delete: Delete an instance of the model
    • list: List all instances of the model
  • Defining routes with Laravel Route Attributes: Use spatie/laravel-route-attributes to clearly and concisely define routes in the controller. For example:
#[Get(uri: 'models/{id}', name: 'models.read')]
public function read(string $id): JsonResponse
{
    return response()->json($this->service->read($id));
}

Benefits:

  • Fast and efficient: Avoid repetition of tasks by automatically generating the files necessary to implement a RESTful API.

  • Clean and organized code: By following the Repository pattern and separating data access logic from business logic, code consistency and maintainability is promoted.

  • Instant Functional API: After running the command and performing the necessary migrations, your API will be ready to use immediately.

  • Flexibility and customization: Although default files are generated, they can be modified according to the specific needs of the project.

Usage

  1. Install the package using Composer:
composer require oscabrera/model-repository
  1. Run the command to generate the API:
php artisan make:repository DummyModel --seed --migration --service --controller --request --factory 

Alternatively, you can use the --all option to generate all available components at once:

php artisan make:repository DummyModel --all
  1. Customize the generated code according to your needs.

In the Model:

Is important use properties $fillable and $hidden in your model.

in $hidden you have to add id in your model.

protected $hidden = [
        'id',
    ];

In $fillable you should add all the properties you want to be returned by your model.

  1. Migrate the database:
php artisan migrate
  1. And ready! You can now access your full RESTful API for the model.

File location:

The files generated by the make:repository command are created inside a folder with the name of the given model.

├── app
│   ├── Contracts
│   │   ├── Repositories
│   │   │   └── Name
│   │   │       └── INameRepository.php
│   │   └── Services
│   │       └── Name
│   │           └── INameService.php
│   ├── Http
│   │   ├── Controllers
│   │   │     └── Name
│   │   │         └── NameController.php
│   │   └── Requests
│   │         └── Name
│   │             └── NameRequest.php
│   ├── Models
│   │   └── Name
│   │       └── Name.php
│   ├── Repositories
│   │   └── Name
│   │       └── NameRepository.php
│   └── Services
│       └── Name
│           └── NameService.php
├── database
│   ├── migrations
│   │   └── 2024_03_17_022718_create_names_table.php
│   ├── Seeders
│   │   └── NameSeeder.php
│   └── Factories
│       └── DummyModel
│           └── DummyModelFactory.php

Benefits of organizing by folders:

Organization: Keeps code organized and easy to find.

Modularity: Allows the logic of each component of the API to be separated.

Maintenance: Facilitates maintenance and updating of the code.

Conclusion:

This Laravel package allows you to create complete Restful APIs quickly and easily, with a better development experience thanks to Spatie Ignition.

Description

This project provides a complete implementation of a model repository using the Laravel framework. The goal is to facilitate the development of complete RESTful APIs for your Laravel models, with CRUD (Create, Read, Update, Delete) functionality and filtering and sorting options.

Technologies:

  • Laravel
  • VitePress
  • PHP

Characteristics:

  • Automatic generation of complete RESTful API for your models.
  • Support for CRUD (Create, Read, Update, Delete).
  • Filtering and sorting options.
  • Validation of input data.
  • Complete documentation with VitePress.

Use:

  • The documentation is organized into sections and chapters.
  • You can browse the documentation using the side menu or the search bar.
  • Each section contains detailed information on a specific VitePress topic.
  • The tutorials will guide you through the steps necessary to perform common tasks.
  • The API reference provides you with complete information on all VitePress features and options.

Contributions:

  • Contributions to this project are appreciated.
  • If you find any bugs or have any suggestions, feel free to create an issue on GitHub.

keywords:

Laravel, RESTful API, Repository Patter, CRUD, API, Repositories, Services, File generation

Additional resources:

oscabrera/model-repository 适用场景与选型建议

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

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

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

围绕 oscabrera/model-repository 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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