frontier/repository
Composer 安装命令:
composer require frontier/repository
包简介
Laravel Frontier Repositories Package
README 文档
README
Frontier Repository
Repository Pattern with Transparent Caching for Laravel
Installation • Quick Start • Caching • API Reference • Commands
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
- Follow PSR-12 coding standards
- Use Laravel Pint for code styling
- Write tests using Pest
- Add strict types to all PHP files
📄 License
MIT License - see LICENSE for details.
👤 Author
Mohamed Khedr — 0xkhdr@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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 frontier/repository 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
Structure for the Laravel Service-Repository Pattern
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Base Skeleton for Laravel Application
Generates a trait to help ease the access of custom repo methods
统计信息
- 总下载量: 82
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-20