laravel - 依赖注入和IoC容器 [英] laravel - dependency injection and the IoC Container

查看:135
本文介绍了laravel - 依赖注入和IoC容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图围绕依赖注入和IoC容器,我正在使用我的UserController作为一个例子。我正在定义UserController在其构造函数中所依赖的,然后使用App :: bind()将这些对象绑定到它。如果我正在使用Input :: get()的外观/方法/东西,我没有利用请求对象,我只是注入它?我应该使用以下代码,现在请求对象被注入还是将Input :: get()解析到同一个Request实例?我想使用静态外观,但是如果它们解析为未注入的对象,则不使用。

I'm trying to wrap my head around dependency injection and the IoC container and i'm using my UserController as an example. I'm defining what the UserController depends on in its constructor and then am binding those objects to it using 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? Should i be using the following code instead, now that the Request object is injected or doesInput::get() resolve to the same Request instance? I'd like to use the static facades but not if they resolve to un-injected objects.

$this->request->get('email');

依赖注入

<?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');
}


推荐答案

如果你担心一个可测试性的事情,那么你应该只是注意到没有通过立面进行路由的实例,因为外墙本身可以测试(这意味着你可以在L4中模拟这些)。您不应该需要注入响应,哈希表,查看环境,请求等。通过它的外观,您只需要注入 $ user

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). 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.

对于其他一切,我只是坚持使用静态外观。查看基础上的交换 shouldReceive 方法 Facade 课程。您可以轻松地使用自己的嘲弄对象来交换底层实例,或者简单地开始嘲笑,例如 View :: ShouldReceive()

For everything else I'd just stick to using the static facade. Check out the swap and shouldReceive methods on the base Facade class. You can easily swap out the underlying instance with your own mocked object or simply start mocking with, for example, View::shouldReceive().

希望这有帮助。

这篇关于laravel - 依赖注入和IoC容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆