php - laravel - dependency injection and the IoC Container -
i'm trying wrap head around dependency injection , ioc container , i'm using usercontroller example. i'm defining usercontroller depends on in constructor , binding objects using app::bind(). if i'm using input::get() facade/method/thing not taking advantage of request object injected it? should using following code instead, request object injected or doesinput::get() resolve same request instance? i'd use static facades not if resolve un-injected objects.
$this->request->get('email');
dependency injection
<?php app::bind('usercontroller', function() { $controller = new usercontroller( new response, app::make('request'), app::make('view'), app::make('validator'), app::make('hash'), new user ); return $controller; });
usercontroller
<?php class usercontroller extends basecontroller { protected $response; protected $request; protected $validator; protected $hasher; protected $user; protected $view; public function __construct( response $response, \illuminate\http\request $request, \illuminate\view\environment $view, \illuminate\validation\factory $validator, \illuminate\hashing\bcrypthasher $hasher, user $user ){ $this->response = $response; $this->request = $request; $this->view = $view; $this->validator = $validator; $this->hasher = $hasher; $this->user = $user; } public function index() { //should use this? $email = input::get('email'); //or this? $email = $this->request->get('email'); //should use this? return $this->view->make('users.login'); //or this? return view::make('users.login'); }
if you're worried testability thing should injecting instances aren't being routed through facade, facades testable (meaning can mock these in l4). shouldn't need inject response, hasher, view environment, request, etc. looks of need injecting $user
.
for else i'd stick using static facade. check out swap
, shouldreceive
methods on base facade
class. can swap out underlying instance own mocked object or start mocking with, example, view::shouldreceive()
.
hope helps.
Comments
Post a Comment