Please note that before aksing this questions I've looked into:
laravel-4 way to inject an object that requires configuration into a controller
Laravel 4: Passing data from make to the service provider
and
Laravel 4: Confused about how to use App::make()
But I cannot see that they answer my question!
Domain Model: A Site must have an Owner and currently this owner is of type Customer but can change in the future to be a Reseller or an Employee etc.. so I'm using Polymorphism as follows:
class Site has an Owner Property. Owner is an interface, Customer is a class which implements Owner, Customer has a Name and a Uid therefore these are required in the constructor of Customer.
Now I want to bind the Customer implementation to the Owner interface so that whenever I call Owner I get a Customer.
We would normally do this:
class UseCustomerAsSiteOwnerServiceProvider extends ServiceProvider{
public function register()
{
$this->app->bind('hidden\Domain\Model\Site\Owner', function ($app)
{
return $this->app->make(new Customer());
});
}
}
However Customer() requires $id and $name to be initialized and these are variable objects - not static so I don't know how to pass them?
I tried this:
public function register()
{
$this->app->bind('hidden\Domain\Model\Site\Owner', function ($app) use ($id, $name)
{
return $this->app->make(new Customer($id, $name));
});
}
But in this case the $name and $id in the USE statement are not defined. Anyone has ideas?
UPDATE: The Customer Constructor is as follows:
/**
* Initialise a new Customer
*
* @param Identifier $id
* @param CustomerName $name
*/
public function __construct(Identifier $id, $name)
{
$this->id = $this->setId($id);
$this->name = $this->setName($name);
}
This will be initialized from an Aggregate root (Site). The correct types will be passed to it.