项目作者: caffeinated

项目描述 :
:factory: Reusable repository interface
高级语言: PHP
项目地址: git://github.com/caffeinated/repository.git
创建时间: 2015-12-23T18:46:26Z
项目社区:https://github.com/caffeinated/repository

开源协议:MIT License

下载


This package has been abandoned and is no longer maintained.

Caffeinated Repository

Source
License

Getting Started

Introduction

The Caffeinated Repository package allows the means to implement a standard boilerplate repository interface. This covers the standard Eloquent methods in a non-static, non-facade driven way right out of the box. Fear not though Batman! The Caffeinated Repository package does not limit you in any way when it comes to customizing (e.g overriding) the provided interface or adding your own methods.

Installing Caffeinated Repository

It is recommended that you install the package using Composer.

  1. composer require caffeinated/repository

This package is compliant with PSR-1, PSR-2, and PSR-4. If you find any compliance oversights, please send a patch via pull request.

Using Repositories

Create a Model

Create your model like you normally would. We’ll be wrapping our repository around our model to access and query the database for the information we need.

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model
  4. class Book extends Model
  5. {
  6. //
  7. }

Create a Repository

Create a new Repository class - usually these classes are simply stored within a Repositories directory. There are a few requirements for each repository instance:

  • Repository classes must extend the Caffeinated EloquentRepository class.
  • Repository classes must specify a public property pointing to the model.
  • Repository classes must specify an array of cache tags. These tags are used by the package to handle automatic cache busting when relevent values change within the database.
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Book;
  4. use Caffeinated\Repository\Repositories\EloquentRepository;
  5. class BookRepository extends EloquentRepository
  6. {
  7. /**
  8. * @var Model
  9. */
  10. public $model = Book::class;
  11. /**
  12. * @var array
  13. */
  14. public $tag = ['book'];
  15. }

Injecting a Repository

Once you’ve built and configured your repository instance, you may inject the class within your classes where needed:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Repositories\BookRepository;
  4. class BookController extends Controller
  5. {
  6. /**
  7. * @var BookRepository
  8. */
  9. protected $book;
  10. /**
  11. * Create a new BookController instance.
  12. *
  13. * @param BookRepository $book
  14. */
  15. public function __construct(BookRepository $book)
  16. {
  17. $this->book = $book;
  18. }
  19. /**
  20. * Display a listing of all books.
  21. *
  22. * @return Response
  23. */
  24. public function index()
  25. {
  26. $books = $this->book->findAll();
  27. return view('books.index', compact('books'));
  28. }
  29. }