什么是服务提供者,我们先看下官方文档的解释,能看懂英文就看英文,直译成中文后更难懂。
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.
服务提供者是所有 Laravel 应用程序启动的中心,你自己写的组件,以及Laravel所有的核心服务,都是通过服务提供者启动的。
But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
但是,我们这里说的"bootstrapped(启动,引导)" 是什么意思呢? 通常是指注册事物,包含注册服务容器绑定,事件监听器,中间件,甚至是路由,服务提供者是设置你的应用程序的中心所在。
If you open the config/app.php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application. Of course, many of these are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.
如果你打开Laravel 的config/app.php文件,你会看见一个 providers
的数组,数组里面所有的所有的服务提供者的类都会被加载到你的应用程序中,当然这里面的很有服务提供者类都是延迟加载的,延迟加载的意思是它们不是在每一个请求的时候都会加载,只有当你的请求会用到这个服务提供者类,它才会被加载。
上面是官方文档的解释,如果看懂了,下面可以不用看了。
服务提供者Service Providers,通过上一节的讲解,现在看到这个名字应该不会特别的陌生了,上一节我们说到laravel的核心是iOC容器,我们在Laravel要用到别的组件,那实现需要将其注册绑定到IOC容器上,我们将这一块绑定的操作独立出来,然后取个名字,它就是服务提供者Service Providers。
它就像路由文件一样,所有的路由我们都统一放在路由文件中,那所有的对IOC容器进行绑定的操作 App::bind()
,我们也把它单独放在的几个地方,比如说,在 app/Providers/
中都是一些服务提供者类,每一个服务提供者都是将其组件绑定到服务容器中,因为现在每一个服务提供者都是写成一个文件的,所以这些服务提供者的文件要与laravel框架建立关系,我们也可以把这个动作叫做注册服务提供者到laravel, 在 config/app.php
中有一个 providers
的一位数组,如下:
'providers' => [ /* * LaravelFrame work 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\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\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, ],
写好的服务提供者需要放进这个数组里,让laravel启动的时候加载它们,这样laravel就真正的通过服务提供者的绑定容器的功能,将各组件给绑定进来了。
我们先来看看Laravel的框架所用的组件是如何通过Service Provider绑定进来的,我们打开 vendor/laravel/
目录