当前位置:网站首页>Laravel service container (Application of context binding)
Laravel service container (Application of context binding)
2022-07-29 05:47:00 【Liaoshengping】

Context binding , According to different strategies, dependency injection is agreed service .
The example on the official website shows that , Different file types , Different storage schemes are used .
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');
});
It's easy to understand , What other scenarios can this be used in , How to use it ? Here's an example .
Example
For example, I want to deliver a batch of e-commerce products , To ship Fruits , And nuts . The timeliness of fruits is relatively strong , Therefore, it is recommended to deliver Shunfeng . If the shelf life of nuts is long, it's ok , I choose a Zhongtong .
Code implementation
Create an interface :
<?php
namespace App\Contracts;
interface Express
{
/** * Access to third parties The price of logistics configuration * @return mixed */
public function getAmount();
}
At present, I only need to get the express price .
Create SF :
<?php
namespace App\Services\express;
use App\Contracts\Express;
class Shunfeng implements Express
{
public function getAmount()
{
return 20;
}
}
Tact
<?php
namespace App\Services\express;
use App\Contracts\Express;
class Yuantong implements Express
{
public function getAmount()
{
return 10;
}
}
stay Providers/AppServiceProvider.php Of register Method to add binding information
$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);
You can see from the registration information above , If the controller of the fruit is called , At the same time, the interface is Express When , Inject the instance of SF . If the controller of the nut is called , At the same time, the interface is Express When , An example of injecting circularity .
controller :
<?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();
}
}
Here is an example of a fruit , When routing to Fruit When the controller , It will inject the instance of SF just defined .
stay routers/web.php Add routing information
Route::get('fruit',[\App\Http\Controllers\Fruit::class,'get']);
Route::get('nut',[\App\Http\Controllers\Nut::class,'get']);
request /fruit The data obtained is :
request /nut The data obtained is :
git Source code
边栏推荐
- 使用Qss设置窗体样式
- 熊市下PLATO如何通过Elephant Swap,获得溢价收益?
- Common characteristic engineering operations
- PHP如何生成二维码?
- Laravel Swagger添加访问密码
- 7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润
- What is sqlmap and how to use it
- Build msys2 environment with win10
- 华为2020校招笔试编程题 看这篇就够了(上)
- 改哭了,终于解决了Cannot read properties of undefined (reading ‘parseComponent‘)
猜你喜欢
![[electronic circuit] how to select ADC chip](/img/41/05153704986117ce9adaa09cb71a7a.png)
[electronic circuit] how to select ADC chip

机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit

Common prompt pop-up box of uniapp

DAO赛道异军突起,M-DAO的优势在哪里?

July 28 ens/usd Value Forecast: ENS attracts huge profits

Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)

7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润

How can Plato obtain premium income through elephant swap in a bear market?

Laravel服务容器(继承与事件)

Qtcreator+cmake compiler settings
随机推荐
Madonna "hellent" bought $1.3 million NFT boring ape, which is now considered too expensive
性能对比|FASS iSCSI vs NVMe/TCP
与开源项目同步开发& CodeReview & Pull Request & fork怎么拉取原始仓库
DAY6:利用 PHP 编写登陆页面
Seay源代码审计系统
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
What is nmap and how to use it
DAY5:PHP 简单语法与使用
Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect
Similarities and differences between REM and PX and EM
Qt设置背景图片方法
Move protocol global health declaration, carry out the health campaign to the end
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
Read and understand move2earn project - move
Sliding switch of tab of uniapp component
Thinkphp6管道模式Pipeline使用
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration
Common prompt pop-up box of uniapp
闪贷Dapp的调研及实现
Related knowledge of elastic box