laravel服务提供者ServiceProvider
(0).服务在laravel中,用以提供一些功能,比如认证功能,比如路由功能,我们可以自定义服务,一个完整的服务包括约束服务的一个接口(也叫协约),一个服务类,一个服务提供者类,服务提供者类负责注册服务类到容器中。
涉及到的文件:
| 文件名称 | 描述 |
| app/Contracts/TestContract.php | 定义约束服务的接口 |
| app/Services/TestService.php | 实际工作的服务类 |
| app/Providers/TestServiceProvider.php | 注册服务到容器的服务提供者类 |
| config/app.php | 注册服务提供者到配置文件 |
| app/Http/Controllers/TestController.php | 测试服务类的控制器 |
| app/Http/routes.php | 配置控制器路由 |
(1).为创建服务提供者,需要先有个服务类,而服务类被一个接口所约束。因此,我们先创建一个接口(协约):
<?php
namespace App\Contracts;
interface TestContract{
public function callMe($controller);
}
------------------------------------------------------
(2)实现服务的协约(接口),实际的服务类:
<?php
namespace App\Services;
use App\Contracts\TestContract;
class TestService implements TestContract{
public function callMe($controller){
echo "hello from TestService and execute from controller ".$controller;
}
}
------------------------------------------------------
(3)创建服务提供者类,用以下命令:
php artisan make:provider TestServiceProvider
修改为如下:
use Illuminate\Support\ServiceProvider;
use App\Services\TestService;
class TestServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//使用singleton绑定单例,方式一
$this->app->singleton('test',function(){
return new TestService();
});
//使用bind绑定实例到接口以便依赖注入,方式二
$this->app->bind('App\Contracts\TestContract',function(){
return new TestService();
});
}
}
------------------------------------------------------
(4)注册服务提供者到config/app.php文件
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
....
App\Providers\TestServiceProvider::class,
];
------------------------------------------------------
(5)现在基本上可以用了,现在创建一个控制器进行测试:
php artisan make:controller TestController
修改文件内容:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
如果没有下面的那句会报错的
use App; //Class 'App\Http\Controllers\App' not found
use App\Contracts\TestContract;
class TestController extends Controller
{
public function __construct(TestContract $test){
//依赖注入
$this->test = $test;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//当前方式是通过新建服务类实例使用服务|是第三步骤的方式二
//$test = App::make('test');
//$test->callMe('TestController');
//当前使用方式为,通过单例使用自定义的服务|是第步骤的方式一
$this->test->callMe('工作找不到可以回家种田');
}
------------------------------------------------------
(6)配置控制器的路由:
Route::resource('test','TestController');
------------------------------------------------------
that's all
浙公网安备 33010602011771号