Laravel8
2020-10-28 11:25:08 0 举报
AI智能生成
测试发布
作者其他创作
大纲/内容
服务提供者
生成命令
php artisan make:provider RiakServiceProvider
编写服务提供者
注册方法:register
引导方法:boot
bindings 和 singletons:通过服务提供者,批量把类注入到容器
注册服务提供者
延迟服务提供者
服务容器
绑定
简单绑定
$this->app->bind('HelpSpot\API', function ($app) {
return new \HelpSpot\API($app->make('HttpClient'));
});
return new \HelpSpot\API($app->make('HttpClient'));
});
绑定一个单例
$this->app->singleton('HelpSpot\API', function ($app) {
return new \HelpSpot\API($app->make('HttpClient'));
});
return new \HelpSpot\API($app->make('HttpClient'));
});
绑定实例
$api = new \HelpSpot\API(new HttpClient);
$this->app->instance('HelpSpot\API', $api);
$this->app->instance('HelpSpot\API', $api);
绑定接口到实现
$this->app->bind(
'App\Contracts\EventPusher',
'App\Services\RedisEventPusher'
);
'App\Contracts\EventPusher',
'App\Services\RedisEventPusher'
);
绑定上下文
绑定基本值
解析
make
$api = $this->app->make('HelpSpot\API');
resolve
$api = resolve('HelpSpot\API');
事件
$this->app->resolving(function ($object, $app) {
// 当容器解析任何类型的对象时调用...
});
$this->app->resolving(\HelpSpot\API::class, function ($api, $app) {
// 当容器解析类型为 "HelpSpot\API" 的对象时调用...
});
// 当容器解析任何类型的对象时调用...
});
$this->app->resolving(\HelpSpot\API::class, function ($api, $app) {
// 当容器解析类型为 "HelpSpot\API" 的对象时调用...
});
请求周期
0 条评论
下一页