当前位置:网站首页>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源码
边栏推荐
- 【无标题】
- Relative positioning and absolute positioning
- 365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
- Day 2
- ClickHouse学习(一)ClickHouse?
- Sqlmap是什么以及使用方法
- 使用Qss设置窗体样式
- Database operation day 6
- Relationship between redrawing and reflow
- Thrift安装手册
猜你喜欢
[C language series] - three methods to simulate the implementation of strlen library functions
【TypeScript】深入学习TypeScript对象类型
Masscan使用教程.
ClickHouse学习(四)SQL操作
Day 2
php写一个购买全网最低价的纸尿裤
ClickHouse学习(九)clickhouse整合mysql
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
Liang Yuqi, founder of aitalk: the link between image and virtual reality
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
随机推荐
WIN10 编译ffmpeg(包含ffplay)
Relationship between redrawing and reflow
DAY6:利用 PHP 编写登陆页面
表格与表单相关知识点总结
Hcia-r & s self use notes (27) comprehensive experiment
ClickHouse学习(四)SQL操作
uniapp页面标题显示效果
【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
rem与px与em异同点
Similarities and differences between REM and PX and EM
ClickHouse学习(十一)clickhouseAPI操作
公众号不支持markdown格式文件编写怎么办?
Selenium实战案例之爬取js加密数据
虚拟增强与现实第二篇 (我是一只火鸟)
[C language series] - detailed explanation of file operation (Part 1)
Selection options of uniapp components (such as package selection)
ClickHouse学习(六)语法优化
Clickhouse learning (IX) Clickhouse integrating MySQL
QT layout management -- Part stretch principle and sizepolicy
Three handshakes and four waves for the interview summary