laravel 服务容器service container和服务提供者service provider


1.创建一个自己的的服务容器

php artisan make:provider EnvatoCustomServiceProvider

这个命令会在app\providers目录下创建一个EnvatoCustomServiceProvide.php 文件

2,注册服务容器


在 config/app.file 修改providers数组:通知laravel启动的时候记得加载我哟,

'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
namespaceApp\Http\Controllers;
  
useApp\Http\Controllers\Controller;
useApp\Library\Services\Contracts\CustomServiceInterface;
  
classTestControllerextendsController
{
    publicfunctionindex(CustomServiceInterface$customServiceInstance)
    {
        echo$customServiceInstance->doSomethingUseful();
    }
}

这样调用CustomServiceInterface使用的就是DemoOne,想要用Demo只需要把TwoEnvatoCustomServiceProvider.php里红色的DemoOne换成DemoTwo就可以了。

大概意思是你家有,你,你爹,你兄弟,工厂到你家只招一个人,你爹有个接口是 儿子A,工厂记录的也是儿子A,想派你去就把儿子A改成你,明天想

派他去就改成你兄弟。工厂只认爹。


说完了register(),说boot()

boot()可以扩展laravel的核心功能(疑问),接触到已被注册的service(疑问),在这个函数里注册时间监听,当某些事情发生就触发(疑问








 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值