我的团队喜欢构造函数注入依赖项的想法,因为它使 deps 在查看类时非常清晰。通过使用外观,我知道它们可以被嘲笑和swap
ped,但是必须检查类的每一行才能弄清楚它依赖于什么!我发现我可以在外观后面找到真正的类,例如Form::getFacadeRoot()
.
我最终得到的控制器代码是:
use Illuminate\Html\FormBuilder as Form;
use Illuminate\Validation\Factory as Validator;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage as Session;
use Illuminate\Http\Request as Input;
use Illuminate\Routing\Redirector as Redirect;
use Illuminate\View\Environment as View;
class HomeController extends BaseController {
protected $form;
protected $validator;
protected $session;
protected $input;
protected $redirect;
protected $view;
protected $layout = 'layouts.master';
protected $validationRules = array(
'name' => array('required', 'min:3'),
'email' => array('required', 'regex:/^.+@.+\..{2,4}$/')
);
public function __construct(Form $form, Validator $validator, Session $session,
Input $input, Redirector $redirect, View $view
) {
$this->form = $form;
$this->validator = $validator;
$this->session = $session;
$this->input = $input;
$this->redirect = $redirect;
$this->view = $view;
}
...
}
当我的测试执行$this->client->request('Get', '/');
时,它会出错:
Illuminate\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]].
我在这里甚至接近正确的轨道吗?我正在编造这个,因为我没有看到太多关于这个问题的讨论。随意评论我尝试的理由;我可以在外墙出售,但是。
谢谢 !