Created
April 4, 2017 09:31
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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