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
Enter fullscreen mode Exit fullscreen mode

Optionally publish the config file:

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

โœจ Artisan Commands

You can generate everything interactively:

php artisan cr:make
Enter fullscreen mode Exit fullscreen mode

Or individually:

php artisan cr:make-repo Product
php artisan cr:make-service Product
Enter fullscreen mode Exit fullscreen mode

It also supports nested paths:

php artisan cr:make-repo Admin\\User
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Convention-Based Binding

Just follow this structure:

app/
โ”œโ”€โ”€ Contracts/
โ”‚   โ””โ”€โ”€ Repositories/
โ”‚       โ””โ”€โ”€ UserRepositoryInterface.php
โ”œโ”€โ”€ Repositories/
โ”‚   โ””โ”€โ”€ UserRepository.php
Enter fullscreen mode Exit fullscreen mode

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

public function __construct(UserRepositoryInterface $userRepo) {
    $this->userRepo = $userRepo;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Testing

Run tests:

composer test
Enter fullscreen mode Exit fullscreen mode

With coverage:

composer test-coverage
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›  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)