我有一个API,并且使用sanctum API令牌作为身份验证。
-假设我收到了一个用户的请求。
-基于用户,我从db获取用户的设置。
-用户设置“ selected_class” =“ foo”。
如何声明将“ selected_class”作为实现注入的服务提供者?
在您的ServiceProvider的注册方法中:
public function register()
{
$this->app()->singleton()->(YOUR_CONTRACT::class, function(){
if(request('certain_field')){
return new CONCRETE_IMPLEMENTATION_1::class;
}
else{
return new CONCRETE_IMPLEMENTATION_2::class;
}
});
}
在您的控制器中,您将像这样通过方法注入来注入合同/接口:
public function someMethod(YOUR_CONTACT::class $concreteImplementation){
// it will be correct implementation of your contract/interface
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句