mnshankar / repository
可重用仓库接口 - 基于 Caffeinated/repository,修改为与 Laravel 5.0/php 5.4 兼容
Requires
- php: >=5.4.0
- illuminate/config: 5.0.*
- illuminate/support: 5.0.*
This package is not auto-updated.
Last update: 2024-09-23 16:00:53 UTC
README
这是 LARAVEL 5.0(使用 PHP 5.4)的 Caffeinated Repository 包的分支
入门指南
简介
Caffeinated Repository 包实现了一个标准的样板仓库接口。它以非静态、非门面驱动的方式直接支持标准的 Eloquent 方法。不过,不用担心!Caffeinated Repository 包在自定义(例如覆盖)提供的接口或添加自己的方法方面不会对你有任何限制。
如果你想要数据库结果缓存工作,请注意,你需要一个支持“标签”的缓存解决方案(例如 Redis/Memcached)。此包会自动缓存通过仓库检索到的数据(60分钟)。它还处理通过仓库更新的数据的缓存失效(基于标签)。事实上,这正是 Caffeinated/repository 需要使用带有标签功能的缓存存储的原因——它使得从缓存中删除数据块变得容易。
安装 Caffeinated Repository
建议您使用 Composer 安装此包。
composer require mnshankar/repository
重要:同时将服务提供者添加到您的 app/config.php 文件中——这设置事件处理,以便通过仓库的更新使缓存无效,从而强制重新查询。
'Caffeinated\Repository\RepositoryServiceProvider'
此包符合 PSR-1、PSR-2 和 PSR-4。如果您发现任何符合性疏忽,请通过拉取请求发送补丁。
使用仓库
创建模型
像平常一样创建您的模型。我们将围绕我们的模型包装仓库以访问和查询数据库以获取所需的信息。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model class Book extends Model { // }
创建仓库
创建一个新的仓库类——通常这些类存储在 Repositories 目录中。每个仓库实例有一些要求
- 仓库类必须扩展 EloquentRepository 类。
- 仓库类必须指定一个指向模型的公共属性。
- 仓库类必须指定一个包含缓存标签的数组。这些标签用于包在数据库中相关值更改时处理自动缓存失效。
<?php namespace App\Repositories; use App\Models\Book; use Caffeinated\Repository\Repositories\EloquentRepository; class BookRepository extends EloquentRepository { /** * @var Model */ public $model = Book::class; /** * @var array */ public $tag = ['book']; }
注入仓库
一旦您构建并配置了仓库实例,您可以在需要的地方将类注入到您的控制器类中
<?php namespace App\Http\Controllers; use App\Repositories\BookRepository; class BookController extends Controller { /** * @var BookRepository */ protected $book; /** * Create a new BookController instance. * * @param BookRepository $book */ public function __construct(BookRepository $book) { $this->book = $book; } /** * Display a listing of all books. * * @return Response */ public function index() { $books = $this->book->findAll(); return view('books.index', compact('books')); } }
src/Contracts/Repository.php 中定义的接口详细说明了 EloquentRepository 类实现的方法。以下列出了这些方法,供您方便参考
public function find($id, $columns = ['*'], $with = []);
public function findBy($attribute, $value, $columns = ['*'], $with = []);
public function findAll($columns = ['*'], $with = []);
public function findWhere($where, $columns = ['*'], $with = []);
public function findWhereBetween($attribute, $values, $columns = ['*'], $with = []);
public function findWhereIn($attribute, $values, $columns = ['*'], $with = []);
public function findWhereNotIn($attribute, $values, $columns = ['*'], $with = []);
public function findOrCreate($attributes);
//RAISES EVENTS 'tag'.entity.creating and 'tag'.entity.created
public function create($attributes);
//RAISES EVENTS 'tag'.entity.updating and 'tag'.entity.updated
public function update($id, $attributes);
//RAISES EVENTS 'tag'.entity.deleting and 'tag'.entity.deleted
public function delete($id);
public function with($relationships);
public function orderBy($column, $direction = 'asc');
//BE CAREFUL WHEN YOU USE PLUCK.. SEMANTICS HAVE CHANGED BETWEEN Laravel 5.0 AND FUTURE VERSIONS
public function pluck($column, $key = null);
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
public static function __callStatic($method, $parameters);
public function __call($method, $parameters);
“Listeners/RepositoryEventListener”中的事件监听器负责在创建、更新和删除时使缓存无效。请注意,您可以使用以下方式手动使任何与您的仓库相关的缓存无效
$repositoryObject->flushCache();
尽管这些方法应该能够满足您80%的需求,但您可以在仓库类中定义您应用程序所需的任何其他方法。有关实现详情,请参阅 EloquentRepository 类。