fomvasss / laravel-repository
此包已废弃,不再维护。未建议替代包。
用于构建 Laravel 仓库的库
1.2.1
2018-06-14 19:53 UTC
Requires
- php: >=7.0.0
- illuminate/support: 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*
README
构建 Laravel 仓库模式和缓存查询的基础类和方法
安装
运行
composer require fomvasss/laravel-repository
发布配置
php artisan vendor:publish --provider="Fomvasss\Repository\Providers\RepositoryServiceProvider" --tag="repository-config"
所有基础方法仓库
- 接口中所有方法请见 RepositoryInterface
- 实现接口请见 BaseRepository
使用
创建自己的仓库
扩展您的仓库类为下一个 BaseRepository
类
<?php namespace App\Repositories; use App\Models\Article; use Fomvasss\Repository\Eloquent\BaseRepository; class ArticleRepository extends BaseRepository { public function model() { return Article::class; } //... }
使用仓库方法
<?php namespace App\Http\Controllers; use App\Repositories\ArticleRepository; class ArticleController extends BaseController { protected $repository; public function __construct(ArticleRepository $repository) { $this->repository = $repository; } public function index() { $articles = $this->repository ->scopes(['byStatus', 1], ['sortable', ['id'=>'desc']], 'searchable') ->with('user') ->where('created_at', \Carbon\Carbon::yesterday(), '>') ->orderBy('created_at') ->get(); //.... } public function show() { $article = $this->repository ->scope('byStatus', 1) ->with(['user', 'categories']) ->where('created_at', \Carbon\Carbon::today(), '<') ->orderBy('created_at') ->firstOrFail(); //.... } //.... }
在自己的仓库中创建自定义方法
! 自定义方法不使用仓库缓存!
public function myCustomMethodByType($attributes) { $this->applyExtras(); $models = $this->query; if (!empty($attributes['type'])) { $models = $this->query->where('type', $attributes['type']); } $this->unsetClauses(); return $models; }
仓库事件
仓库实体有下列事件
RepositoryEntityCreated
RepositoryEntityUpdated
RepositoryEntityDeleted
例如,您可以在您的 EventServiceProvider 中添加以下内容
protected $listen = [ \Fomvasss\Repository\Events\RepositoryEntityCreated::class => [ \App\Listeners\CreatedNewModelInRepoListener::class ] ];
并在方法 handle(在您的监听器 CreatedNewModeInRepoListener
)中使用以下方法
public function handle(RepositoryEntityCreated $event) { dd($event->getAction()); dd($event->getModel()); dd($event->getRepository()); }
使用仓库缓存
所有缓存方法请见接口 CacheableInterface
示例带有缓存的仓库
<?php namespace App\Repositories; use App\Models\Article; use Fomvasss\Repository\Contracts\CacheableInterface; use Fomvasss\Repository\Eloquent\BaseRepository; use Fomvasss\Repository\Traits\CacheableRepository; class ArticleRepository extends BaseRepository implements CacheableInterface { use CacheableRepository; protected $cacheTime = 60; protected $cacheTimeForMethod = [ 'all' => 10, 'get' => 10, 'paginate' => 10, 'find' => 1, ]; protected $cacheOnly = ['all', 'get', 'find']; public function model() { return Article::class; } }
关闭缓存的中间件
示例在路由中使用中间件
添加到 App\Http\Kernel.php
protected $routeMiddleware = [ //... 'rpc-off' => \Fomvasss\Repository\Http\Middleware\RepositoryCacheOff::class, ];
并在您的路由中使用
Route::get('article', ArticleController@index)->middleware(['rpc-off']);
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。