定制 frontier/repository 二次开发

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

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

frontier/repository

Composer 安装命令:

composer require frontier/repository

包简介

Laravel Frontier Repositories Package

README 文档

README

Frontier Repository

Repository Pattern with Transparent Caching for Laravel

InstallationQuick StartCachingAPI ReferenceCommands

Latest Version PHP Version Laravel Version

Features

  • Repository Pattern — Clean abstraction for data access
  • Decorator Caching — Choose between simple or cached repository implementations
  • Full CRUD — Create, Read, Update, Delete with consistent API
  • Advanced Queries — Filtering, sorting, pagination, scopes
  • Module Support — Works with internachi/modular

Installation

composer require frontier/repository

Quick Start

1. Create Interface

It is best practice to always code against interfaces.

php artisan frontier:repository-interface UserRepository

2. Generate Repository

Create a standard repository that implements the interface.

php artisan frontier:repository UserRepositoryEloquent

3. Generate Repository Cache (Optional)

Create a decorator repository that adds caching.

php artisan frontier:repository-cache UserRepositoryCache

4. Bind in ServiceProvider

Bind your interface to either the standard repository or the cached one.

// app/Providers/RepositoryServiceProvider.php

// Option A: Standard Repository (No Caching)
$this->app->bind(UserRepository::class, function ($app) {
    return new UserRepositoryEloquent(new User());
});

// Option B: Cached Repository (Repository + Caching Decorator)
$this->app->bind(UserRepository::class, function ($app) {
    return new UserRepositoryCache(
        new UserRepositoryEloquent(new User())
    );
});

Caching

Caching is implemented via the Decorator Pattern. The BaseRepositoryCache wraps your BaseRepository and handles caching logic transparently.

Architecture

┌─────────────────────────────────────────┐
│         UserRepository         │
└───────────────────┬─────────────────────┘
                    │ bind to either:
    ┌───────────────┴───────────────┐
    ▼                               ▼
UserRepositoryEloquent              UserRepositoryCache
extends BaseRepository              extends RepositoryCache
(Direct DB Access)                  (Caching Decorator)

Usage

Inject the interface into your controllers or actions:

class UserController extends Controller
{
    public function __construct(
        protected UserRepository $users
    ) {}

    public function index()
    {
        // Automatically cached if UserRepositoryCache is bound
        return $this->users->retrieve();
    }
}

Cache Control Methods

The BaseRepositoryCache exposes helper methods to control cache behavior:

// Skip cache for this query
$users->withoutCache()->retrieve();

// Force refresh cache
$users->refreshCache()->retrieve();

// Clear all cache
$users->clearCache();

Caching Behavior

Method Behavior
retrieve() Cached (Read)
retrievePaginate() Cached (Read)
find() Cached (Read)
findOrFail() Cached (Read)
count() Cached (Read)
exists() Cached (Read)
create() Invalidates Cache
update() Invalidates Cache
delete() Invalidates Cache
updateOrCreate() Invalidates Cache
insert() Invalidates Cache
upsert() Invalidates Cache

Configuration

Publish config:

php artisan vendor:publish --tag=repository-config
// config/repository-cache.php
return [
    'enabled' => env('REPOSITORY_CACHE_ENABLED', true),
    'driver' => env('REPOSITORY_CACHE_DRIVER', null),
    'ttl' => env('REPOSITORY_CACHE_TTL', 3600),
];

Artisan Commands

Command Description
frontier:repository {name} Create standard repository
frontier:repository-cache {name} Create cached repository decorator
frontier:repository-interface {name} Create repository interface
frontier:repository-action {name} Create repository action

All commands support the --module flag for modular applications.

CRUD Operations

// CREATE
$user = $this->users->create(['name' => 'John']);

// READ
$user = $this->users->find(['id' => 1]);
$users = $this->users->retrieve();
$users = $this->users->retrievePaginate(['*'], ['per_page' => 15]);

// UPDATE
$count = $this->users->update(['id' => 1], ['name' => 'Jane']);

// DELETE
$count = $this->users->delete(['id' => 1]);

Advanced Queries

The retrieve() and retrievePaginate() methods accept an $options array to build complex queries without writing boilerplate.

$users = $this->users->retrieve(['id', 'name', 'email'], [
    // Filtering (requires EloquentFilter on Model)
    'filters' => ['status' => 'active', 'role' => 'admin'],
    
    // Scopes
    'scopes' => ['verified', 'olderThan' => [18]],
    
    // Relationships
    'with' => ['profile', 'posts'],
    'with_count' => ['posts'],
    
    // Sorting
    'sort' => 'created_at',
    'direction' => 'desc',
    
    // Pagination (for retrievePaginate)
    'per_page' => 25,
    
    // Limits & Offsets
    'limit' => 10,
    'offset' => 5,
    
    // Grouping
    'group_by' => ['status'],
    'distinct' => true,
]);

Supported Options

Option Description Example
filters Apply Eloquent filters ['status' => 'active']
scopes Apply local scopes ['active', 'type' => ['admin']]
with Eager load relations ['profile']
with_count Count relations ['comments']
sort Order by column 'created_at'
direction Order direction 'desc'
per_page Items per page 15
limit Limit results 10
offset Offset results 5
distinct Distinct selection true
joins Join tables ['posts' => ['users.id', '=', 'posts.user_id']]

Note

To use filters, your Eloquent Model must use the Filterable trait (typically from tucker-eric/eloquentfilter).

Development

composer test          # Run tests
composer lint          # Fix code style
composer rector        # Apply refactorings

Related Packages

Package Description
frontier/frontier Laravel Starter Kit
frontier/action Action Pattern
frontier/module Modular Architecture

🤝 Contributing

  1. Follow PSR-12 coding standards
  2. Use Laravel Pint for code styling
  3. Write tests using Pest
  4. Add strict types to all PHP files

📄 License

MIT License - see LICENSE for details.

👤 Author

Mohamed Khedr0xkhdr@gmail.com

Made with ❤️ for the Laravel community

frontier/repository 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-20