简体   繁体   English

laravel - 依赖注入和IoC容器

[英]laravel - dependency injection and the IoC Container

I'm trying to wrap my head around dependency injection and the IoC container and i'm using my UserController as an example. 我试图围绕依赖注入和IoC容器,我正在使用我的UserController作为示例。 I'm defining what the UserController depends on in its constructor and then am binding those objects to it using App::bind(). 我正在定义UserController在其构造函数中所依赖的内容,然后使用App :: bind()将这些对象绑定到它。 If i'm using the Input::get() facade/method/thing am i not taking advantage of the Request object i just injected into it? 如果我正在使用Input :: get()facade / method / thing我没有利用我刚刚注入的Request对象? Should i be using the following code instead, now that the Request object is injected or doesInput::get() resolve to the same Request instance? 我是否应该使用以下代码,现在注入Request对象或者doInput :: get()解析为同一个Request实例? I'd like to use the static facades but not if they resolve to 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 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 i use this?
    $email = Input::get('email');
    //or this?
    $email = $this->request->get('email');

    //should i use this?
    return $this->view->make('users.login');

    //or this?
    return View::make('users.login');
}

If you're worried about a testability thing then you should really only be injecting instances that aren't being routed through a facade, as the facades themselves are testable already (meaning you can mock these in L4). 如果您担心可测试性问题,那么您应该只注入未通过外观路由的实例,因为外观本身已经可测试(意味着您可以在L4中模拟这些)。 You shouldn't need to inject the response, hasher, view environment, request, etc. By the looks of it you only need to be injecting the $user . 您不需要注入响应,哈希,查看环境,请求等。从它的外观来看,您只需要注入$user

For everything else I'd just stick to using the static facade. 对于其他一切,我只是坚持使用静态外观。 Check out the swap and shouldReceive methods on the base Facade class. 查看基本Facade类上的swapshouldReceive方法。 You can easily swap out the underlying instance with your own mocked object or simply start mocking with, for example, View::shouldReceive() . 您可以使用自己的模拟对象轻松地交换基础实例,或者只是使用View::shouldReceive()开始View::shouldReceive()

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM