the repository pattern

Repository mewakili layer arsitektur yang menghubungkan antara aplikasi dan sumber data. Pola ini adalah pola yang paling banyak digunakan sebagai titik utama sari aplikasi yang tidak harus mengetahui sumber data mana yang harus diimplementasikan dan bagaimana cara implementasinya. Ini menjadikannya lebih mudah beralih dari sumber data yang satu ke sumber data yang lain. Atau menerapkan perubahan struktural ke sumber data yang ada.

Pola repository ini memperkenalkan repository interface yang mendefinisikan bagaimana sebuah aplikasi dan sumber data harus terhubung atau berkomunikasi. Setiap sumber data memiliki kelompok tersendiri yang mengimplementasikan repository interface. Kelompok pengontrol akan memanggil metode yang ditentukan dalam repository interface dan tidak akan tahu bagaimana dan dari mana data diambil.

Pertama, implementasikan dalam Laravel. Disini kita akan membuat sebuah repository interface. Interface ini dapat diletakkan dimana saja, namun dalam contoh ini, kita akan menggunakan struktur directory berikut :

– app/Repositories/Contracts: All repository interfaces will be stored here
– app/Repositories/Eloquent: All interface Eloquent implementations can be found here
– app/Repositories/Providers: Service providers for repositories are saved in this folder

<?php
namespace App\Repositories\Contracts;

interface ProductRepositoryInterface
{
public function search($name);

public function getAllByUser($user_id);

public function getAllByCategory($category_id);
}

Perhatikan bahwa setiap entitas akan memiliki repository interface masing-masing. Dalam contoh ini menunjukkan repository interface untuk entitas produk dan itu diimplementasikan dengan Eloquent :

<?php
namespace App\Repositories\Eloquent;

use App\Repositories\Contracts\ProductRepositoryInterface;
use App\Entity\Product;

class ProductRepository implements ProductRepositoryInterface
{

public function search($name)
{
return Product::where(‘title’, ‘LIKE’, ‘% ‘ . $name . ‘%’)
->get();
}

public function getAllByUser($user_id)
{
return Product::where(‘user_id’, ‘=’, $user_id)
->get();
}

public function getAllByCategory($category_id)
{
return Product::where(‘category_id’, ‘=’, $category_id)
->get();
}
}

Untuk dapat menggunakan kelompok ini dalam framework, kita perlu mendaftar ke layanan provider.

<?php
namespace App\Repositories\Providers;
use Illuminate\Support\ServiceProvider;
class ProductRepositoryServiceProvider extends ServiceProvider
{
   public function register()
   {
      $this->app->bind(
         'App\Repositories\Contracts\ProductRepositoryInterface',
         // To change the data source, replace this class name
         // with another implementation
         'App\Repositories\Eloquent\ProductRepository'
      );
   }
}

 

Akhirnya, layanan provider perlu ditambahkan ke file konfigurasi:
<?php
...
'providers' => [

   /*
    * Laravel Framework Service Providers...
    */
   Illuminate\Auth\AuthServiceProvider::class,
   Illuminate\Broadcasting\BroadcastServiceProvider::class,
   Illuminate\Bus\BusServiceProvider::class,
   Illuminate\Cache\CacheServiceProvider::class,
   Illuminate\Foundation\Providers\
      ConsoleSupportServiceProvider::class,
   Illuminate\Cookie\CookieServiceProvider::class,
   Illuminate\Database\DatabaseServiceProvider::class,
   Illuminate\Encryption\EncryptionServiceProvider::class,
   Illuminate\Filesystem\FilesystemServiceProvider::class,
   Illuminate\Foundation\Providers\FoundationServiceProvider::class,
   Illuminate\Hashing\HashServiceProvider::class,
   Illuminate\Mail\MailServiceProvider::class,
   Illuminate\Pagination\PaginationServiceProvider::class,
   Illuminate\Pipeline\PipelineServiceProvider::class,
   Illuminate\Queue\QueueServiceProvider::class,
   Illuminate\Redis\RedisServiceProvider::class,
   Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
   Illuminate\Session\SessionServiceProvider::class,
   Illuminate\Translation\TranslationServiceProvider::class,
   Illuminate\Validation\ValidationServiceProvider::class,
   Illuminate\View\ViewServiceProvider::class,

   /*
    * Application Service Providers...
    */
   App\Providers\AppServiceProvider::class,
   App\Providers\EventServiceProvider::class,
   App\Providers\RouteServiceProvider::class,

   App\Repositories\Providers\
      ProductRepositoryServiceProvider::class,
],
Pada titik ini, repository dapat diterapkan ke kelompok pengontrol. Pengontrol contoh akan terlihat seperti ini:
<?php
namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use App\Repositories\Contracts\ProductRepositoryInterface;
class ProductController extends BaseController
{
   protected $productRepository;

   // $productRepository will call the methods from the
   // class defined above in the service provider
   public function __construct(ProductRepositoryInterface
      $productRepository)
   {
      $this->productRepository = $productRepository;
   }

   public function search(Request $request)
   {
      $name = $request->input('name');
      $products = $this->productRepository->search($name);
      return view('home.index', ['products' => $products]);
   }
}
Selamat mencoba...
Salam Sukses

Sumber :
The Repository Pattern in PHP
Vojislav Janjic