derakht/repository-pattern

Implement repository pattern design with a command

V1.0.0 2020-09-21 14:39 UTC

This package is auto-updated.

Last update: 2024-10-23 01:10:46 UTC


README

Generate repository structure with single command

This package generates repository pattern files related to a model to use them in your app.

Installation

Require this package with composer using the following command:

composer require derakht/repository-pattern

Configuration

Publish the configuration file with th following command.

php artisan vendor:publish --provider="Derakht\RepositoryPattern\RepositoryPatternServiceProvider"

You can change default paths in this file.

Usage

You can now run the below command to create repository files.

php artisan make:repository ModelName

Example

php artisan make:repository Post

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class PostController extends Controller
{
    public $postRepository;

    public function __construct()
    {
        $this->postRepository = app(PostRepositoryInterface::class);
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}

or if you want to use injection add App\Providers\RepositoryServiceProvider::class to provider array in config/app.php.

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class ReportController extends Controller
{
    public $postRepository;

    public function __construct(PostRepositoryInterface $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}