当前位置:网站首页>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源码
边栏推荐
- QT setting background image method
- ClickHouse学习(二)ClickHouse单机安装
- Realize simple database query (incomplete)
- 【电子电路】ADC芯片如何选型
- php写一个购买全网最低价的纸尿裤
- Liang Yuqi, founder of aitalk: the link between image and virtual reality
- Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
- [C language series] - print prime numbers between 100 and 200
- DAY4:MySQL 数据库的建立及简单实用
- Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
猜你喜欢

【TypeScript】深入学习TypeScript函数

Hcia-r & s self use notes (24) ACL
![[typescript] in depth study of typescript functions](/img/0c/e838960c8efd6e87046d35f8942a07.png)
[typescript] in depth study of typescript functions

QT setting background image method

uniapp组件之tab选项卡滑动切换

Day 2

Day14: upload labs customs clearance tutorial

QT layout management -- Part stretch principle and sizepolicy

第五空间智能安全⼤赛真题----------PNG图⽚转换器

Day 3
随机推荐
[C language series] - three methods to simulate the implementation of strlen library functions
Day 1
Day 5
Clickhouse learning (XI) clickhouseapi operation
ClickHouse学习(五)集群操作
Wechat applet - component parameter transmission, state management
Introduction to C language array to proficiency (array elaboration)
JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
TXT 纯文本操作
微信小程序-屏幕高度
DAY5:PHP 简单语法与使用
[C language series] - string + partial escape character explanation + annotation tips
DAY4:SQL Sever 简单使用
link与@import导入外部样式的区别
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
DAY13:文件上传漏洞
Clickhouse learning (IX) Clickhouse integrating MySQL
sql-server 数据表的简单操作
QFrame类学习笔记
组件传参与生命周期