流程概述:
1、接口的创建
2、接口服务类的创建
3、接口提供者的创建,并且绑定接口和接口服务类
4、接口提供者的注册
5、控制器的创建
控制器:
use App\Contracts\SuperInterface;//接口
class SuperMan extends ApiController {public $superman; //依赖注入
public function __construct(SuperInterface $superman){
$this->superman = $superman;
}
public function index() {
$this->superman->callMe('SuperManController');
}
}
接口:
interface SuperInterface{
public function callMe($controller);
}
接口服务类,implements了SuperInterface接口
use App\Contracts\SuperInterface;
class SuperManService implements SuperInterface{
public function callMe($controller) {
dd('Call Me From TestServiceProvider In '.$controller);
}
}
服务提供者
SuperManServiceProvider
public function boot() {
//使用bind绑定实例到接口以便依赖注入,奖接口和服务类绑定
$this->app->bind('App\Contracts\SuperInterface','App\Services\SuperMan\SuperManService');
}
注册服务提供者
app/config
providers中加入
App\Providers\SuperManServiceProvider::class