laravel的核心概念:服务提供者provider解析
我们知道laravel和核心就是一个IoC容器, 被称之为服务容器. 那么作为IoC容器, 就必须要有绑定对象生成器的工作. 在laravel中是服务提供者来项服务容器中绑定对象生成器的。
百牛信息技术bainiu.ltd整理发布于博客
下面就继续说说, laravel的核心架构中的 服务提供者.provider
服务提供者, 就是负责在laravel的IoC容器中绑定对象生成器的代码. 在laravel中称之为服务提供者.
注册服务提供者
laravel要知道, 哪些有服务提供者来负责绑定IoC容器, 采用的办法是, 在配置文件中, 将所有的服务提供者配置好, 所有的服务提供者, 都在 config/app.php中配置, 示例代码如下 : config/app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
config/web.app
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Package Service Providers...
*/
//
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
],
|
上面代码的每一行, 都注册一个一个服务提供者.
服务提供者绑定对象生成器到IoC服务容器
那么服务器提供者, 是如何绑定IoC容器中的对生成器呢?
每个服务器提供者对象, 都要实现一个register()方法, 该方法会被laravel自动调用. register()方法, 就是用来向IoC服务容器绑定对象生成器的.
以lluminate\Cache\CacheServiceProvider这个提供者为例, 查看其register方法, 位于:vendor/laravel/framework/src/Illuminate/Cache/CacheServerProvider.php文件中, 代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
vendor/laravel/framework/src/Illuminate/Cache/CacheServerProvider.php
class CacheServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('cache', function ($app) {
return new CacheManager($app);
});
$this->app->singleton('cache.store', function ($app) {
return $app['cache']->driver();
});
$this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
});
$this->registerCommands();
}
}
|
在register()方法中, 通过$this->app访问到IoC服务容器. 然后调用该容器的singleton()方法, 绑定单例对象生成器.
可见, 当register()被调用时, 该服务提供者就会向IoC服务容器绑定内容. 绑定完毕后, laravel中就可以使用该类的对象了. make()也好, 自动注入依赖也罢都可以了.
结语
这样, 服务提供者的核心任务, 绑定对象的生成代码到IoC服务容器这个任务就完成了. 这就是laravel中的, 服务提供者provider的概念.
一家之言, 欢迎讨论拍砖.