Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatteoOreficeIT/130cd02e34dcc908fb24b5322c75f360 to your computer and use it in GitHub Desktop.
Save MatteoOreficeIT/130cd02e34dcc908fb24b5322c75f360 to your computer and use it in GitHub Desktop.
A pseudo-code example of using laravel IoC recursive dependency resolution with parameters (Container::makeWith)
<?php
use Illuminate\Support\Facades\App;
use Illuminate\Database\Eloquent\Model;
class Capacity extends Model {
// ...
}
class Supplier {
public $capacity;
/**
* Supplier constructor.
* @param Capacity $c
*/
public function __construct(Capacity $c)
{
$this->capacity = $c;
}
}
App::bind('supplier', function($app,$params){
import($params);
// for example we can load related capacity from DB depending on parameters
$capacity = Capacity::findBySupplierId($id,$country);
return new Supplier($capacity);
});
$supplier = App::makeWith('supplier',['country'=>'IT','id'=>12]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment