当前位置:网站首页>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
边栏推荐
- Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
- 我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
- DAY13:文件上传漏洞
- 一文读懂Move2Earn项目——MOVE
- 超简单集成HMS ML Kit 实现parental control
- 熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
- Record the SQL injection vulnerability of XX company
- table中同一列中合并相同项
- Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)
- July 28 ens/usd Value Forecast: ENS attracts huge profits
猜你喜欢

JDBC连接数据库详细步骤

The LAAS protocol of defi 2.0 is the key to revitalizing the development of defi track

从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值

Crypto巨头们ALL IN元宇宙,PlatoFarm或能突围

华为2020校招笔试编程题 看这篇就够了(下)

与开源项目同步开发& CodeReview & Pull Request & fork怎么拉取原始仓库

DAY15:文件包含漏洞靶场手册(自用 file-include 靶场)

IDEA使用JDBC连接MySQL数据库个人详细教程

Go|Gin 快速使用Swagger

量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
随机推荐
The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
Mobile terminal -flex item attribute
DAY5:PHP 简单语法与使用
2022 mathematical modeling competition summer training lecture - optimization method: goal planning
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
Win10 compiles ffmpeg (including ffplay)
win10+opencv3.2+vs2015配置
大部分PHP程序员,都搞不懂如何安全代码部署
Basic concepts of MySQL + database system structure + extended application + basic command learning
DAY4:MySQL 数据库的建立及简单实用
Fvuln automated web vulnerability detection tool
Crypto giants all in metauniverse, and platofarm may break through
DAY6:利用 PHP 编写登陆页面
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
Seay source code audit system
DAY6:利用 PHP 编写文件上传页面
Thinkphp6管道模式Pipeline使用
Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
如何 Pr 一个开源composer项目
table中同一列中合并相同项