Generate repository structure with single command
This package generates repository pattern files related to a model to use them in your app.
Require this package with composer using the following command:
composer require derakht/repository-pattern
Publish the configuration file with th following command.
php artisan vendor:publish --provider="Derakht\RepositoryPattern\RepositoryPatternServiceProvider"
You can change default paths in this file.
You can now run the below command to create repository files.
php artisan make:repository ModelName
php artisan make:repository Post
<?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
.
<?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();
}
}