定制 l0n3ly/laravel-repository-with-service 二次开发

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

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

l0n3ly/laravel-repository-with-service

最新稳定版本:v2.0.0

Composer 安装命令:

composer require l0n3ly/laravel-repository-with-service

包简介

Repository and service pattern package for Laravel

README 文档

README

Latest Version Total Downloads License Laravel Boost Compatible

A Laravel package that scaffolds the repository and service pattern — generates repository and service classes with interfaces, and automatically binds them to their implementations via the container.

✨ Features

  • 🏗️ Artisan Scaffolding - Generate repositories and services with make:repository and make:service
  • 🔗 Auto Binding - Automatically binds interfaces to implementations via the service container
  • 📁 Subdirectory Support - Organize classes in nested directories (e.g., Admin/UserRepository)
  • 🔄 Paired Generation - Generate a repository and service together with a single command
  • 📋 Interface-First - Always generates a contract interface alongside each implementation
  • 🎨 Multiple Templates - Choose between API service template or blank template
  • 🤖 Boost Compatible - Full support for Laravel Boost AI-powered workflows and Copilot skills

📋 Requirements

  • PHP ^8.2
  • Laravel 11, 12, or 13

📦 Installation

Install the latest 1.x release:

composer require l0n3ly/laravel-repository-with-service

Publish the config file:

php artisan vendor:publish --tag=repository-with-service-config

Quick Start

Fastest Start: Generate Everything at Once

Scaffold a complete feature with one command:

php artisan make:model User --all

This creates everything — model, migration, factory, seeder, controller, repository, and service.

Generate a Repository

# Basic repository
php artisan make:repository User

# Repository with paired service
php artisan make:repository User --service

# Repository with API-style service
php artisan make:repository User --service --api

# Repository in subdirectory
php artisan make:repository Admin/User --service

Generate a Service

# Basic service
php artisan make:service UserService

# API service with response helpers
php artisan make:service UserService --api

# Service with paired repository
php artisan make:service UserService --repository

# Minimal service
php artisan make:service UserService --blank

Generate from a Model

# Model with service and repository
php artisan make:model Product --service --repository

# Model with everything (fastest)
php artisan make:model Order --all

Use in Your Code

Services and repositories are automatically bound to the container:

class UserController extends Controller
{
    public function __construct(
        protected UserRepository $repository,
        protected UserService $service
    ) {}

    public function index()
    {
        return $this->service->getActiveUsers();
    }
}

📖 Documentation

Comprehensive documentation is available:

Document Purpose
Full Guide Installation, quick start, and all commands
API Reference Core interfaces and configuration
Troubleshooting Common issues and solutions
Examples Real-world usage examples
Contributing Contribution guidelines
Laravel Boost Integration Using with AI-powered Boost workflows

🤖 Laravel Boost Support

This package is fully integrated with Laravel Boost and provides three AI skills to enhance your development workflow:

Three Essential Skills

  • Repository Generator - Generate repositories with best practices
  • Service Generator - Generate services with business logic
  • Service Binding - Understand dependency injection and binding patterns

These skills are located in resources/boost/skills/ and are discovered by running:

php artisan boost:install --discover

Select the desired skills during installation, and they'll be available to your AI agent.

AI-Powered Workflows

Get intelligent suggestions for:

  • Generating complete feature modules
  • Creating repository hierarchies
  • Setting up service structures
  • Testing patterns and mocking

Example Boost Usage

User: "Generate a complete blog module with posts, comments, and categories"

Boost activates the skills and suggests:
✓ PostRepository with PostService (API template)
✓ CommentRepository with CommentService
✓ CategoryRepository with CategoryService
✓ BlogManager service orchestrating operations

Getting Started with Boost

  1. Install Laravel Boost in your Laravel project
  2. Run php artisan boost:install --discover
  3. Select this package's skills when prompted
  4. Ask your AI agent to generate repositories, services, or help with architecture

See BOOST.md for detailed integration guide.

🎯 Common Patterns

Blog Application

php artisan make:repository Post --service --api
php artisan make:repository Category --service
php artisan make:repository Post/Comment --service

E-Commerce

php artisan make:repository Product --service --api
php artisan make:repository Order --service --api
php artisan make:service Shop/Checkout --api
php artisan make:service Shop/Inventory --api

SaaS with Multi-Tenancy

php artisan make:repository Tenant --service --api
php artisan make:repository User --service --api
php artisan make:repository Subscription --service --api

See EXAMPLES.md for complete real-world examples.

🔧 Configuration

Edit config/service-repository.php to customize:

return [
    'repository_directory' => 'app/Repositories',
    'repository_namespace' => 'App\Repositories',
    'service_directory' => 'app/Services',
    'service_namespace' => 'App\Services',
    // ... naming conventions
];

Override Binding Example

Decorate or replace a bound implementation in your service provider:

$this->app->extend(UserRepository::class, function ($service, $app) {
    return new CachedUserRepository($service);
});

Service API Template

The --api flag generates services with response helpers:

class UserServiceImplement implements UserService
{
    use ResultService;

    public function create(array $data): array
    {
        try {
            $user = $this->repository->create($data);
            return $this->success($user, 'User created');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}

🚀 Performance Tips

  1. Use Caching - Wrap repositories with caching decorators
  2. Eager Loading - Use Eloquent's with() in repositories
  3. Singleton Services - For stateless services, bind as singleton
  4. Optimize Queries - Focus repository methods on specific queries

🧪 Testing

Mock repositories in tests:

public function test_service_logic()
{
    $repository = Mockery::mock(UserRepository::class);
    $repository->shouldReceive('find')->with(1)->andReturn(['id' => 1]);

    $this->app->bind(UserRepository::class, fn() => $repository);

    $service = app(UserService::class);
    $result = $service->getUser(1);

    $this->assertEquals(1, $result['id']);
}

📚 Real-World Examples

Check EXAMPLES.md for complete implementations:

  • Blog with posts, comments, and categories
  • E-commerce checkout process
  • Multi-tenant SaaS structure
  • Admin dashboards
  • Testing patterns

🤝 Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

📄 License

The MIT License (MIT). Please see LICENSE.md for more information.

Support

  • 📖 Documentation: See docs/ directory
  • 🐛 Issues: Report on GitHub
  • 💬 Discussions: Ask questions on GitHub Discussions
  • 🔧 Help: Check Troubleshooting Guide

Project guide:

Changelog

See CHANGELOG for release notes.

License

MIT. See LICENSE.md.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固