DEV Community

Cover image for πŸš€ Contract Resolver: Auto-Bind Repositories & Services in Laravel
Taki Elias
Taki Elias

Posted on

πŸš€ Contract Resolver: Auto-Bind Repositories & Services in Laravel

Are you tired of manually binding interfaces to implementations in Laravel? Or creating the same service and repository boilerplate over and over?

Meet Contract Resolver, a Laravel package that handles this for you β€” cleanly and automatically.


🎯 What Is It?

Contract Resolver helps you follow Laravel's Repository + Service pattern by:

  • Auto-binding interfaces to implementations
  • Generating services/repositories/interfaces using artisan commands
  • Supporting nested namespaces
  • Requiring zero dependencies

πŸ”§ Installation

composer require takielias/contract-resolver

Optionally publish the config file:

php artisan vendor:publish --provider="TakiElias\ContractResolver\ContractResolverServiceProvider" --tag="contract-resolver.config"

✨ Artisan Commands

You can generate everything interactively:

php artisan cr:make

Or individually:

php artisan cr:make-repo Product
php artisan cr:make-service Product

It also supports nested paths:

php artisan cr:make-repo Admin\\User

πŸ“ Convention-Based Binding

Just follow this structure:

app/
β”œβ”€β”€ Contracts/
β”‚   └── Repositories/
β”‚       └── UserRepositoryInterface.php
β”œβ”€β”€ Repositories/
β”‚   └── UserRepository.php

And the binding happens automatically! You can inject interfaces like:

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

πŸ§ͺ Testing

Run tests:

composer test

With coverage:

composer test-coverage

πŸ›  Why Use It?

βœ… Clean, testable Laravel apps
βœ… Focus more on business logic
βœ… No need to manually bind contracts
βœ… Fully customizable


πŸ”— Links

If you find this useful, give it a ⭐ on GitHub. I'd love your feedback or contributions!

Top comments (0)