ozankurt/repoist

Laravel ^5.6 仓库生成器。

3.2.0 2019-09-19 09:07 UTC

This package is auto-updated.

Last update: 2024-09-25 00:16:07 UTC


README

Laravel 5.2+ 版本的仓库生成器

使用方法

步骤 1: 通过 Composer 安装

composer require ozankurt/repoist

步骤 2: 发布并编辑配置

在 Laravel 中: 在控制台中运行 php artisan vendor:publish --tag=repoist-config 以根据您的需求配置 Repoist。

步骤 3: 运行 Artisan 命令!

您已设置完毕。在控制台中运行 php artisan,您将看到新命令。

对于 Lumen

bootstrap\app.php 中启用 Facades 和 Eloquent,同时也启用配置文件。

$app->withFacades();
$app->withEloquent();

$app->configure('repoist');

在“注册服务提供者”部分添加

$app->register(Kurt\Repoist\RepoistServiceProvider::class);

示例

仓库

php artisan make:repository Task

将输出

  • app/Contracts/Task/TaskRepository.php (接口)
  • app/Repositories/Eloquent/EloquentTaskRepository.php
  • app/Task.php (如果需要)

条件

php artisan make:criterion Completed

将输出

  • app/Repositories/Eloquent/Criteria/Completed.php

配置

如果您无法在此处通过 artisan 发布 config/repoist.php,您可以复制并使用它。

<?php

return [

	/**
	 * Namespaces are being prefixed with the applications base namespace.
	 */
	'namespaces' => [
	    'contracts' => 'Repositories\Contracts',
	    'repositories' => 'Repositories\Eloquent',
	],

	/**
	 * Paths will be used with the `app()->basePath().'/app/'` function to reach app directory.
	 */
	'paths' => [
	    'contracts' => 'Repositories/Contracts/',
	    'repositories' => 'Repositories/Eloquent/',
	],

];

配置

Kurt\Repoist\Repositories\Eloquent\AbstractRepository 的默认方法。

示例使用

Customer.php

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    /**
     * Customer has many Tickets.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function tickets()
    {
    	return $this->hasMany(Ticket::class, 'customer_id', 'id');
    }
}

EloquentCustomerRepository.php

<?php
namespace App\Repositories\Eloquent;

use App\Models\Customer;
use App\Repositories\Contracts\CustomerRepository;
use Kurt\Repoist\Repositories\Eloquent\AbstractRepository;

class EloquentCustomerRepository extends AbstractRepository implements CustomerRepository
{
    public function entity()
    {
        return Customer::class;
    }
}

PagesController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\Contracts\CustomerRepository;
use Kurt\Repoist\Repositories\Eloquent\Criteria\EagerLoad;

class PagesController extends Controller
{
	private $customerRepository;

	function __construct(CustomerRepository $customerRepository)
	{
		$this->customerRepository = $customerRepository;
	}

    public function getHome()
    {
        $customersWithTickets = $this->customerRepository->withCriteria([
        	new EagerLoad(['tickets']),
        ])->all();

        return $customersWithTickets;
    }
}