定制 jeishanul/laravel-repository-generator 二次开发

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

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

jeishanul/laravel-repository-generator

Composer 安装命令:

composer require jeishanul/laravel-repository-generator

包简介

A Laravel package to generate Repository pattern classes and auto-bind them.

README 文档

README

Latest Version on Packagist GitHub Code Style Action Total Downloads

A simple Laravel Artisan command to scaffold the Repository pattern: generates an Interface in app/Interfaces, a concrete Repository in app/Repositories, and auto-binds them in AppServiceProvider.php. Supports nested namespaces (e.g., Api\Admin\User).

Features

  • Generates CRUD-ready Interface and Repository classes.
  • Handles nested paths (e.g., app/Interfaces/Api/Admin/UserInterface.php).
  • Automatic IoC binding for dependency injection.
  • Overwrite protection with --force flag.
  • Compatible with Laravel 10, 11, and 12.

Installation

You can install the package via Composer:

composer require jeishanul/laravel-repository-generator

The package will auto-discover its service provider. If not, add it manually to config/app.php under providers:

'providers' => [
    // ...
    Jeishanul\RepositoryGenerator\RepositoryGeneratorServiceProvider::class,
],

Usage

Run the command to generate files for a given repository:

php artisan make:repository User

Or with nested namespaces:

php artisan make:repository Api\Admin\User

Options

  • --force: Overwrite existing files.

Generated Files

For php artisan make:repository Api\Admin\User:

  1. Interface: app/Interfaces/Api/Admin/UserInterface.php

    <?php
    
    namespace App\Interfaces\Api\Admin;
    
    interface UserInterface
    {
        /**
         * Get all users.
         */
        public function all();
    
        /**
         * Find a user by ID.
         */
        public function find($id);
    
        /**
         * Create a new user.
         */
        public function create(array $data);
    
        /**
         * Update a user.
         */
        public function update($id, array $data);
    
        /**
         * Delete a user.
         */
        public function delete($id);
    }
  2. Repository: app/Repositories/Api/Admin/UserRepository.php

    <?php
    
    namespace App\Repositories\Api\Admin;
    
    use App\Interfaces\Api\Admin\UserInterface;
    use App\Models\User;
    use Illuminate\Database\Eloquent\Collection;
    
    class UserRepository implements UserInterface
    {
        protected $model;
    
        public function __construct(User $model)
        {
            $this->model = $model;
        }
    
        public function all(): Collection
        {
            return $this->model->all();
        }
    
        public function find($id)
        {
            return $this->model->findOrFail($id);
        }
    
        public function create(array $data): User
        {
            return $this->model->create($data);
        }
    
        public function update($id, array $data): bool
        {
            $model = $this->find($id);
            return $model->update($data);
        }
    
        public function delete($id): bool
        {
            $model = $this->find($id);
            return $model->delete();
        }
    }
  3. Auto-Binding: Appended to app/Providers/AppServiceProvider.php in the register() method:

    public function register()
    {
        // ... existing code ...
    
        $this->app->bind(App\Interfaces\Api\Admin\UserInterface::class, App\Repositories\Api\Admin\UserRepository::class);
    }

Using in Controllers

Inject the interface for loose coupling:

<?php

namespace App\Http\Controllers;

use App\Interfaces\Api\Admin\UserInterface;

class UserController extends Controller
{
    protected $userRepo;

    public function __construct(UserInterface $userRepo)
    {
        $this->userRepo = $userRepo;
    }

    public function index()
    {
        return $this->userRepo->all();
    }

    public function store(Request $request)
    {
        return $this->userRepo->create($request->validated());
    }

    // ... other methods
}

Customization

  • Stubs: Override stubs by publishing them (future feature) or editing src/Stubs/ in your fork.
  • Model Path: Assumes flat models (e.g., App\Models\User). For nested models, update the stub's import.
  • Bindings: Uses AppServiceProvider for simplicity. For production, consider a dedicated RepositoryServiceProvider.

Testing

composer test

(Tests use PHPUnit; add your own for custom stubs.)

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Thank you for considering contributing! Please follow these steps:

  1. Fork the repo on GitHub.
  2. Create a feature branch (git checkout -b feature/new-feature).
  3. Commit changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Open a Pull Request.

Security Vulnerabilities

If you discover a security vulnerability, please email shishirjeishanul@gmail.com instead of opening an issue.

License

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

Credits

Star this repo if it helps your project! Follow @jeishanul on GitHub for more packages.

jeishanul/laravel-repository-generator 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-29