vivek-mistry/repository-interface

Laravel package for dependency injection to separate business logic.

1.0.0 2025-05-20 12:29 UTC

This package is auto-updated.

Last update: 2025-05-20 12:37:35 UTC


README

A Laravel package that leverages dependency injection to cleanly separate business logic from other application layers, promoting better code organization, testability, and adherence to SOLID principles

🛠️ Installation

Install the package via Composer:

composer require vivek-mistry/repository-interface

Particular Model Generare the Repository-Interface

php artisan app:make-repo {ModelName}

So using above two files created at app/Repositories/Interface & app/Repositories/Repository

  • ModelNameInterface.php
  • ModelNameInterfaceRepository.php

Create Service Prvider

php artisan make:provider RepositoryServiceProvider

Register Your Service Provider & register your repository

=> Register your service provider

=> In your RepositoryServiceProvider add below :

    public function boot(): void
    {
        $this->app->bind(
            INTERFACE_NAME::class,
            REPOSITORY_NAME::class
        );
    }

HOW TO USE IN YOUR CONTROLLER?

For example :

class UserController extends Controller
{
    protected $userRepository;

    /**
     * Load Repository
     */
    public function __construct(
        UserRepositoryInterface $userRepository
    ) {
        $this->userRepository = $userRepository;
    }

    public function index($request)
    {
        $data = [
            'name' => $request->name,
            'email' => $request->email
        ];
        $this->userRepository->createOrUpdate($data);
    }
}

Testing

composer test

Credits

License

MIT License. See LICENSE for details.