当前位置:网站首页>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源码
边栏推荐
- Relationship between link and @import
- Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)
- 记xx公司SQL注入漏洞
- Display effect of uniapp page title
- sql-server 数据表的简单操作
- Qtcreator+cmake compiler settings
- DAY6:利用 PHP 编写登陆页面
- DAY5:PHP 简单语法与使用
- What is sqlmap and how to use it
- 微信小程序-组件传参,状态管理
猜你喜欢
随机推荐
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
uniapp之常用提示弹框
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
ClickHouse学习(一)ClickHouse?
Database operation day 6
WIN10 编译ffmpeg(包含ffplay)
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
Li Kou 994: rotten orange (BFS)
Basic use of redis
Summary of knowledge points related to forms and forms
用threejs 技术做游戏跑酷
table中同一列中合并相同项
Hcia-r & s self use notes (24) ACL
Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)
虚拟增强与现实第二篇 (我是一只火鸟)
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration
Wechat applet change attribute value -setdata- bidirectional binding -model
微信小程序更改属性值-setData-双向绑定-model
nmap是什么以及使用教程
DAY5:PHP 简单语法与使用