【发布时间】:2014-05-27 07:52:37
【问题描述】:
我想在 Laravel 4 控制器中制作可测试的代码。我知道 DI(依赖注入),我知道可测试的代码可能看起来像:
class UsersController extends BaseController
{
public function __construct(User $user, Notice $notice)
{
$this->user = $user;
$this->notice = $notice;
}
public function getIndex()
{
...
$this->user
...
$this->notice
...
}
public function getPage()
{
...
$this->user
...
}
}
如您所见,我将两个对象注入到控制器中。
对于getIndex 函数,非常完美,我在其中使用了两个对象。
但请注意,对于getPage 函数,我只使用$this->user,不需要$this->notice。
但是,$this->notice 对象已经被实例化了,这确实不好。
有没有更好的方法不实例化所有对象?
【问题讨论】:
标签: php laravel dependency-injection controller laravel-4