angkosal / repository
Laravel 仓库生成器。
This package is auto-updated.
Last update: 2024-09-29 04:09:04 UTC
README
Laravel 5 Repositories 用于抽象数据层,使我们的应用程序更灵活,易于维护。
你想了解更多关于仓库模式的信息? 阅读这篇优秀的文章。
目录
安装
Composer
执行以下命令以获取该包的最新版本
composer require angkosal/repository
Laravel
>= laravel5.5
ServiceProvider 将自动附加
其他
在你的 config/app.php
中,将 Angkosal\Repository\RepositoryServiceProvider::class
添加到 providers
数组的末尾
'providers' => [ ... Angkosal\Repository\RepositoryServiceProvider::class, ],
发布配置
php artisan vendor:publish --provider="Angkosal\Repository\RepositoryServiceProvider" --tag="config"
方法
Angkosal\Repository\Contracts\RepositoryInterface
- all();
- first();
- find($id);
- findWhere($column, $value);
- findWhereFirst($column, $value);
- findWhereLike($column, $value);
- paginate($perPage = 10);
- create(array $properties);
- update($id, array $properties);
- delete($id);
Angkosal\Repository\Contracts\CriteriaInterface
- withCriteria($criteria)
用法
创建模型
正常创建模型,但定义从输入表单数据可以填充的属性非常重要。
namespace App; class Post extends Eloquent { // or Ardent, Or any other Model Class protected $fillable = [ 'title', 'author', ... ]; ... }
创建仓库
namespace App\Repositories\Eloquent; use App\Post; use Angkosal\Repository\Eloquent\AbstractRepository; class EloquentPostRepository extends AbstractRepository { /** * Specify Model class name * * @return string */ function model() { return Post::class; } }
生成器
通过生成器轻松创建仓库。
配置
您必须首先配置仓库文件的存储位置。默认情况下是 "app" 文件夹和命名空间 "App"。请注意,paths
数组中的值实际上用作 命名空间 和文件路径。不过,生成过程中会处理前后斜杠。
<?php return [ // Namespaces are being prefixed with the applications base namespace. 'namespaces' => [ 'contracts' => 'Repositories\Contracts', 'repositories' => 'Repositories\Eloquent', 'criteria' => 'Repositories\Criteria', ], // Paths will be used with the `app()->basePath().'/app/'` function to reach app directory. 'paths' => [ 'contracts' => 'Repositories/Contracts/', 'repositories' => 'Repositories/Eloquent/', 'criteria' => 'Repositories\Criteria', ], ];
命令
要为您的 Post 模型生成仓库,使用以下命令
php artisan make:repository Post
要为具有 Blog 命名空间的 Post 模型生成仓库,使用以下命令
php artisan make:repository Blog/Post
这将创建新的 provider 调用 RepositoryServiceProvider.php
并自动绑定仓库。在你的 config/app.php 中,将 YOUR_NAMESPACE\Providers\RepositoryServiceProvider::class
添加到 providers 数组的末尾
'providers' => [ ... YOUR_NAMESPACE\Providers\RepositoryServiceProvider::class, ],
完成,刚才已经绑定了其实际仓库的接口,例如在您的 Repositories Service Provider 中。
$this->app->bind('{YOUR_NAMESPACE}Repositories\Contracts\PostRepository', '{YOUR_NAMESPACE}Repositories\Eloquent\EloquentPostRepository');
然后使用
public function __construct({YOUR_NAMESPACE}Repositories\Contracts\PostRepository $repository){ $this->repository = $repository; }
或者,您可以使用 artisan 命令为您执行绑定。
php artisan make:binding Post
使用方法
namespace App\Http\Controllers; use App\Repositories\Contracts\PostRepository; class PostsController extends BaseController { /** * @var PostRepository */ protected $repository; public function __construct(PostRepository $repository){ $this->repository = $repository; } .... }
在仓库中查找所有结果
$posts = $this->repository->all();
带有分页的仓库中查找所有结果
$posts = $this->repository->paginate($limit = 10);
按 ID 查找结果
$post = $this->repository->find($id);
按字段名称查找结果
$posts = $this->repository->findWhere('country_id','15');
按字段名称和第一行查找结果
$posts = $this->repository->findWhereFirst('country_id','15');
在仓库中创建新条目
$post = $this->repository->create( Input::all() );
在仓库中更新条目
$post = $this->repository->update( $id, Input::all() );
在仓库中删除条目
$this->repository->delete($id)
创建标准
使用命令
php artisan make:criteria IsActive
标准是根据您的需求应用特定条件以更改查询的仓库的方式。您可以在仓库中添加多个标准。
use Angkosal\Repository\Contracts\CriterionInterface; class IsActive implements CriterionInterface { public function apply($model) { return $model->where('active', true ); } }
在控制器中使用标准
namespace App\Http\Controllers; use App\Repositories\Contracts\PostRepository; class PostsController extends BaseController { /** * @var PostRepository */ protected $repository; public function __construct(PostRepository $repository){ $this->repository = $repository; } public function index() { $posts = $this->repository ->withCriteria(new MyCriteria1(), new MyCriteria2()) ->all(); ... } }