1.创建一个自己的的服务容器
php artisan make:provider EnvatoCustomServiceProvider
这个命令会在app\providers目录下创建一个EnvatoCustomServiceProvide.php 文件
2,注册服务容器
'providers' => [
/*
* Laravel Framework Service Providers...
*/
//加上下面这一行
App\Providers\EnvatoCustomServiceProvider::class,
],
现在基本的都写好了,但是我们的服务容器是空的,没啥意思,下面加点东西去。3,把app\providers\EnvatoCustomServiceProvider.php修改为:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Library\Services\DemoOne;//这个还没创建,等会创建
class EnvatoCustomServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->bind('App\Library\Services\DemoOne', function ($app) {
//注册App\Library\Services\DemoOne类,用处就是
return new DemoOne();//需要解析DemoOne的时候(比如函数参数为DemoOne $customServiceInstance)
}); //就当是传入一个DemoOne对象
}
}
创建App\Library\Services\DemoOne.php(只是一个类)
<?php
namespace App\Library\Services;
class DemoOne
{
public function doSomethingUseful()
{
return '好难';
}
}
写一下控制器
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Library\Services\DemoOne;
class TestController extends Controller
{
public function index(DemoOne $customServiceInstance)//这里传入DemoOne $customServiceInstance,传入了一个对象
{ //就是为了获得DemoOne类的一些东西
echo $customServiceInstance->doSomethingUseful();
}
}
再把路由设置设置,访问一下TestController,发现打印出来了“好难”,嘻嘻,感觉差不多了。
但是文档说了,如果类没有继承任何接口,没必要这么做,因为即使你不搞什么服务之类的,写好类后直接use引入控制器里不是也可以吗?搞那么多干嘛。让我们看看继承接口后是什么弄的
再在library目录下创建Contracts/CustonServiceInterface.php
<?php
// app/Library/Services/Contracts/CustomServiceInterface.php
namespace App\Library\Services\Contracts;
Interface CustomServiceInterface
{
public function doSomethingUseful();
}
修改DemoOne.php
<?php
// app/Library/Services/DemoOne.php
namespace App\Library\Services;
use App\Library\Services\Contracts\CustomServiceInterface;
class DemoOne implements CustomServiceInterface//继承那个接口
{
public function doSomethingUseful()
{
return 'Output from DemoOne';
}
}
再在DemoOne.php旁边创建一个DemTwo.php
<?php
// app/Library/Services/DemoTwo.php
namespace App\Library\Services;
use App\Library\Services\Contracts\CustomServiceInterface;
class DemoTwo implements CustomServiceInterface//也继承那个接口
{
public function doSomethingUseful()
{
return 'Output from DemoTwo';
}
}
修改EnvatoCustomServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Library\Services\DemoOne;
class EnvatoCustomServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->bind('App\Library\Services\Contracts\CustomServiceInterface', function ($app) {
return new DemoOne();//这里绑定的是接口,一被解析就解析成DemoOne
});
}
}
控制器也改改
<?php
namespace
App\Http\Controllers;
use
App\Http\Controllers\Controller;
use
App\Library\Services\Contracts\CustomServiceInterface
;
class
TestController
extends
Controller
{
public
function
index(
CustomServiceInterface
$customServiceInstance
)
{
echo
$customServiceInstance
->doSomethingUseful();
}
}
这样调用CustomServiceInterface使用的就是DemoOne,想要用Demo只需要把TwoEnvatoCustomServiceProvider.php里红色的DemoOne换成DemoTwo就可以了。
大概意思是你家有,你,你爹,你兄弟,工厂到你家只招一个人,你爹有个接口是 儿子A,工厂记录的也是儿子A,想派你去就把儿子A改成你,明天想
派他去就改成你兄弟。工厂只认爹。
说完了register(),说boot()
boot()可以扩展laravel的核心功能(),接触到已被注册的service(),在这个函数里注册时间监听,当某些事情发生就触发()