Bindify is a Laravel package that provides a simple way to bind interfaces to their implementations using attributes.
You can install the package via composer:
composer require algoyounes/bindify
BindType::Singleton
: Keeps one instance and shares it everywhere.BindType::Transient
: Creates a new instance every time you use it.
- Use the
#[BindWith]
attribute to bind an interface to its implementation
namespace App\Contracts;
use AlgoYounes\Bindify\Attributes\BindWith;
use AlgoYounes\Bindify\Attributes\BindType;
#[BindWith(DefaultService::class, BindType::Singleton)]
interface ServiceContract
{
public function execute();
}
- Create the implementation of the interface
namespace App\Services;
use App\Contracts\ServiceContract;
class DefaultService implements ServiceContract
{
public function execute()
{
// Your implementation here
}
}