$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,怎么绑定呢?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3
$app->when(‘App\Http\Controllers\UserController)->needs($a)->give($avalue);

这样的吧

2年前 评论

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

2年前 评论
deatil 2年前

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