当前位置:网站首页>Laravel服务容器(上下文绑定的运用)
Laravel服务容器(上下文绑定的运用)
2022-07-29 05:19:00 【廖圣平】

上下文绑定,根据不同的策略依赖注入约定好的 服务。
从官网的例子说明了,不同的文件类型,使用的储存方案不一样。
use App\Http\Controllers\PhotoController;
use App\Http\Controllers\UploadController;
use App\Http\Controllers\VideoController;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
$this->app->when(PhotoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('local');
});
$this->app->when([VideoController::class, UploadController::class])
->needs(Filesystem::class)
->give(function () {
return Storage::disk('s3');
});
这个很好理解,这个还可以在什么场景下使用,怎么使用呢?下面举一个例子。
例子
比如我要配送一批电商商品,要配送 水果,和坚果。水果的时效性比较强,所以推荐配送顺丰。坚果保质期长就随便了,我这里选一个中通。
代码实现
创建接口:
<?php
namespace App\Contracts;
interface Express
{
/** * 获取第三方 物流配置的价格 * @return mixed */
public function getAmount();
}
目前我只需要获取快递的价格就好了。
创建顺丰:
<?php
namespace App\Services\express;
use App\Contracts\Express;
class Shunfeng implements Express
{
public function getAmount()
{
return 20;
}
}
圆通
<?php
namespace App\Services\express;
use App\Contracts\Express;
class Yuantong implements Express
{
public function getAmount()
{
return 10;
}
}
在Providers/AppServiceProvider.php 的register 方法中添加绑定信息
$this->app->when('App\Http\Controllers\Fruit')
->needs(Express::class)
->give(Shunfeng::class);
$this->app->when('App\Http\Controllers\Nut')
->needs(Express::class)
->give(Yuantong::class);
从上面的注册信息中可以看到,如果调用了水果的控制器,同时接口为 Express 时候,注入顺丰的实例。如果调用了坚果的控制器,同时接口为 Express 时候,注入圆通的实例。
控制器:
<?php
namespace App\Http\Controllers;
class Fruit
{
public $express;
public function __construct(\App\Contracts\Express $express)
{
$this->express = $express;
}
public function get()
{
return $this->express->getAmount();
}
}
这边举例一个水果,当路由到 Fruit 控制器的时候,会注入刚刚定义的顺丰的实例。
在routers/web.php 添加路由信息
Route::get('fruit',[\App\Http\Controllers\Fruit::class,'get']);
Route::get('nut',[\App\Http\Controllers\Nut::class,'get']);
请求 /fruit 获取到的数据为:
请求 /nut 获取到的数据为:
git源码
边栏推荐
猜你喜欢

ClickHouse学习(十)监控运行指标

About local variables

Clickhouse learning (x) monitoring operation indicators

Introduction to C language array to proficiency (array elaboration)
![[typescript] type reduction (including type protection) and type predicate in typescript](/img/74/52fe769ed3850e01d97cb9fefb7373.png)
[typescript] type reduction (including type protection) and type predicate in typescript

WIN10 编译ffmpeg(包含ffplay)

Win10 compiles ffmpeg (including ffplay)

Installation steps and environment configuration of vs Code
![[C language series] - string + partial escape character explanation + annotation tips](/img/75/698ba0672af9d6118ee7e2fdf6daae.png)
[C language series] - string + partial escape character explanation + annotation tips

第五空间智能安全⼤赛真题----------PNG图⽚转换器
随机推荐
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
TXT 纯文本操作
What is sqlmap and how to use it
微信小程序-组件传参,状态管理
AR虚拟增强与现实
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
[C language series] - three methods to simulate the implementation of strlen library functions
How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
2022 mathematical modeling competition summer training lecture - optimization method: goal planning
移动端-flex项目属性
Do students in the science class really understand the future career planning?
Fvuln-自动化web漏洞检测工具
【无标题】
弹性盒子相关知识
用threejs 技术做游戏跑酷
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
[C language series] - string + partial escape character explanation + annotation tips
Selenium实战案例之爬取js加密数据
ClickHouse学习(五)集群操作
DAY15:文件包含漏洞靶场手册(自用 file-include 靶场)