thaile / repository

该包最新版本(dev-master)没有提供许可证信息。

Laravel 框架插件。

dev-master 2019-01-10 08:52 UTC

This package is not auto-updated.

Last update: 2024-09-27 09:23:30 UTC


README

概述

ThaiLe 仓库用于抽象数据层,使我们的应用程序更易于维护。

您想了解更多关于仓库模式的信息吗?阅读这篇文章

目录

安装

Composer

更新 composer.json 文件

{
    "require": {
        "thaile/repository": "dev-master"
    }
}

在项目根目录下运行以下命令

composer update

Laravel

在 config/app.php 中将 ThaiLe\Repository\Providers\RepositoryServiceProvider::class 添加到 provider 数组的末尾

发布配置

php artisan vendor:publish --provider="ThaiLe\Repository\Providers\RepositoryServiceProvider"

方法

仓库接口

ThaiLe\Repository\Contracts\RepositoryInterface

  • all($columns = array('*'))
  • paginate($limit = null, $columns = ['*'])
  • find($id, $columns = ['*'])
  • findSoftDelete($id, $columns = ['*'])
  • findByField($field, $value, $columns = ['*'])
  • findWhere(array $where, $columns = ['*'])
  • findWhereIn($field, array $where, $columns = [*])
  • findWhereNotIn($field, array $where, $columns = [*])
  • create(array $attributes)
  • update(array $attributes, $id)
  • delete($id)
  • with(array $relations)
  • applyCriteria()
  • pushCriteria()
  • popCriteria()
  • getCriteria()
  • getByCriteria()
  • skipCriteria()
  • resetCriteria()

用法

在您的模型中

创建一个具有可填充属性的模型

namespace App;

use Illuminate\Database\Eloquent

class Book extends Eloquent
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'category', 'price',
    ];
}

在您的仓库中

创建一个仓库接口

namespace App\Repositories\Contracts;

class BookRepositoryInterface
{

}

创建一个仓库

namespace App\Repositories\Eloquents;

use ThaiLe\Repository\Eloquent\BaseRepository;
use App\Book;
use App\Repositories\Contracts\BookRepositoryInterface;

class BookRepository extends BaseRepository implements BookRepositoryInterface
{

    /**
         * Specify Model class name
         *
         * @return string
         */
        function model()
        {
            return Book::class;
        }
}

在 AppServiceProvider 或任何自定义服务提供商的注册中

$this->app->bind(BookRepositoryInterface::class, BookRepository::class);

在您的控制器中

namespace App\Http\Controllers;

use App\Repositories\Contracts\BookRepositoryInterface;

class BookController extends Controller
{

    /**
     * View the listing books.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(BookRepositoryInterface $bookRepository)
    {
        $bookRepository->all();

        return view('home');
    }
}

创建一个 Criteria

Criteria 是通过特定条件更改查询逻辑的一种方式。

  • 创建 MyBookCriteriaInterface
namespace App\Repositories\Contracts\Criteria;

interface MyBookCriteriaInterface {}
  • 在 MyBooksCriteria 中实现
namespace App\Repositories\Eloquents\Criteria;

use ThaiLe\Repository\Contracts\CriteriaInterface;
use ThaiLe\Repository\Contracts\RepositoryInterface;
use App\Repositories\Contracts\Criteria\MyBookCriteriaInterface;

class MyBooksCriteria implements CriteriaInterface, MyBookCriteriaInterface
{
    public function apply($model, RepositoryInterface $repository)
        {
            $model = $model->where('user_id','=', Auth::user()->id );
            return $model;
        }
}

在 AppServiceProvider 或任何自定义服务提供商的注册中

$this->app->bind(MyBookCriteriaInterface::class, MyBooksCriteria::class);

在控制器或任何逻辑文件中使用

namespace App\Http\Controllers;

use App\Repositories\Contracts\BookRepositoryInterface;
use App\Repositories\Contracts\Criteria\MyBookCriteriaInterface;

class BookController extends Controller
{

    /**
     * View the listing books.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(BookRepositoryInterface $bookRepository, MyBookCriteriaInterface $criteria)
    {
        $bookRepository->pushCriteria($criteria);
        $bookRepository->all();

        return view('home');
    }
}

生成器

通过生成器快速创建您的仓库(Repository、RepositoryInterface、Model、Migration 文件)

配置

所有生成器配置都位于 config/repository.php 文件中。

'generator'  => [
        'basePath'      => app_path(),
        'rootNamespace' => 'App\\',
        'paths'         => [
            'repositories' => 'Repositories/Eloquents',
            'interfaces'   => 'Repositories/Contracts',
            'models'       => 'Models',
        ]
    ]

命令

使用以下命令为您的 Book 模型生成仓库

php artisan make:repository Book

生成包含一些可填充字段的仓库

php artisan make:repository Book --fillable="string:title,text:content"