$app->when()->needs('$variableName') '$variableName'是什么?

$app->when(‘App\Http\Controllers\UserController’)->needs(‘$variableName’)->give($value);

$value 这是什么?
‘$variableName’ 这是什么?文档没写

这样写不可以啊
$this->app->when(TestService::class)->needs(‘string’)->give(function(){return [‘1111’];});

文档这样介绍的:

$app->when(‘App\Http\Controllers\UserController’)->needs(‘$variableName’)->give($value);

原始值绑定

逻辑是某个类需要某个变量时解析某个值

这样写就可以
$this->app->when(TestService::class)->needs(CouponRepositoryInterface::class)->give(function($app){
return $app->make(CouponRepository::class);
});

TestService的构造函数必须制定接口 CouponRepositoryInterface。
give(必须是一个实例)

那文档说 “逻辑是某个类需要某个变量时解析某个值”

那TestService的构造函数只需要一个变量$a,怎么绑定呢?

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3
$app->when(‘App\Http\Controllers\UserController)->needs($a)->give($avalue);

这样的吧

3年前 评论

@deatil 是的,可以了。可以变量和接口实现一起吗。 例如UserController 构造函数__construct(interface $f,$a,$b,$c) 这样的容器要如何板顶

3年前 评论
deatil 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!