且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们如何在依赖注入控制器中添加值?

更新时间:2023-12-01 10:23:16

Laravel的IoC容器可让您为不同的类指定不同的解析器.

Laravel's IoC container lets you specify different resolvers for different classes.

使用when()->needs()->give()流:

$container->when('UserModel')->needs('DAO')->give(function () {
    return new DAO('connectionA', 'usernameA', 'passwrodA');
});

$container->when('PostModel')->needs('DAO')->give(function () {
    return new DAO('connectionB', 'usernameB', 'passwrodB');
});

请参见文档.查找标题为上下文绑定的部分.

See the docs. Look for the section titled Contextual Binding.