laravel5.5 自定义defer service provider的写法

场景

  • 像文档描述的那样,defer service provider 不是没有每个请求都要加载的,所以可以提高应用的响应效率。defer
    service provider只会在需要的时候才会加载。 那么应该怎么操作

参考文档

解决

  • YourServiceProvider需要设置 $defer 属性 && provides 方法
    • protected $defer = true;
    • The provides method should return the service container bindings registered by the provider 翻译一下就是providers 需要返回service container
  • 验证是否设置成功
    • php artisan clear-compile 清除掉编译的文件
    • 发送一次请求 打开bootstrap/cache/services.php 文件, 就可以再deferred下标中找到自己注册的service provider 了
  /**
     * Get the services provided by the provider.
     *
     * @return array
     */
   public function provides()
    {
        return [Connection::class];
    }

举例

<?php

namespace App\Providers;

use App\Billing\Stripe;
use Illuminate\Support\ServiceProvider;

class BillServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('billing', function($app){
           return new Stripe();
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['billing'];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值