定制 kamarkoding/kamarkoding-repository 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kamarkoding/kamarkoding-repository

最新稳定版本:1.3.2

Composer 安装命令:

composer require kamarkoding/kamarkoding-repository

包简介

Repository generator for Laravel

README 文档

README

Generator sederhana untuk membuat Repository Pattern di Laravel secara otomatis.
Package ini membantu menjaga arsitektur aplikasi tetap rapi, terstruktur, dan mengikuti prinsip SOLID.

Fitur Utama

  • Membuat Repository Interface dan Repository Class hanya dengan satu perintah.
  • Struktur folder otomatis dibuat:
    • app/Repository/Contracts
    • app/Repository/Eloquent
  • Binding otomatis interface → implementasi (tanpa perlu menambahkan di AppServiceProvider)
  • Menggunakan Laravel Package Auto-Discovery (tanpa daftar provider manual).
  • Kode bersih, ringan, dan cocok untuk aplikasi kecil maupun besar.

Instalasi

1. Tambahkan repository (jika lokal)

Jika package disimpan secara lokal:

"repositories": [
    {
        "type": "path",
        "url": "../kamarkoding-repository"
    }
]
  1. Install melalui Composer
composer require kamarkoding/kamarkoding-repository

Laravel otomatis mendaftarkan Service Provider melalui auto-discovery.

Jika auto-discovery dimatikan, daftar manual di config/app.php:

'providers' => [
    Kamarkoding\KamarkodingRepository\Providers\RepositoryServiceProvider::class,
];

Membuat Repository Baru

Gunakan perintah berikut:

php artisan make:repository User

Output :

Created: Class UserRepositoryInterface.php
Created: Class UserRepository.php
Repository created successfully.
app/
└── Repository/
    ├── Contracts/
    │   └── UserRepositoryInterface.php
    └── Eloquent/
        └── UserRepository.php

Struktur Folder

Struktur lengkap setelah membuat repository:

app/
└── Repository/
    ├── Contracts/
    │   └── <Name>RepositoryInterface.php
    └── Eloquent/
        └── <Name>Repository.php

File: Interface

<?php

namespace App\Repository\Contracts;

interface UserRepositoryInterface
{
    //
}

File: Implementasi

<?php

namespace App\Repository\Eloquent;

use App\Repository\Contracts\UserRepositoryInterface;

class UserRepository implements UserRepositoryInterface
{
    //
}

Binding Otomatis

Package ini otomatis menghubungkan:

UserRepositoryInterface::class → UserRepository::class

Tidak perlu lagi menambahkan:

$this->app->bind(UserRepositoryInterface::class, UserRepository::class);

Penggunaan dalam Controller Laravel

<?php

namespace App\Http\Controllers;

use App\Repository\Contracts\UserRepositoryInterface;

class UserController extends Controller
{
    protected $Users;

    public function __construct(UserRepositoryInterface $Users)
    {
        $this->Users = $Users;
    }

    public function index()
    {
        $data = $this->Users->getAll(); // contoh method
        return view('Users.index', compact('data'));
    }
}

Penggunaan di Livewire Component

<?php

use Livewire\Component;
use App\Repository\Contracts\UserRepositoryInterface;

protected UserRepositoryInterface $userRepository;

class UserIndex extends Component
{
    public UserRepositoryInterface $Users;

    /**
    * Inject repository langsung dari container.
    */
    public function mount(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function delete($id)
    {
        $this->Users->delete($id);
    }

    public function render()
    {
        return view('livewire.user.index');
    }
}

Atau menggunakan resolver otomatis:

public function getRepository()
{
    return app(UserRepositoryInterface::class);
}

Penjelasan Arsitektur

Repository Pattern membantu:

  1. Memisahkan business logic dari data layer
  2. Mengurangi duplikasi query
  3. Meningkatkan testability (mocking jadi mudah)
  4. Memperjelas struktur project Memudahkan perubahan dari Eloquent ke Query Builder atau API tanpa mengubah Controller

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-17