当前位置:网站首页>Laravel service container (inheritance and events)
Laravel service container (inheritance and events)
2022-07-29 05:48:00 【Liaoshengping】

The container is written in the front Context binding , Next, look at Inheritance and events
stay Laravel In the frame , Sometimes it needs to be modified Basic documents of the system , But modify vender A file in a directory , It must be an unwise choice , For the expansibility of the framework , You can modify or overwrite the value to be modified through the inheritance or event of the container .
For example, I want to set db Of setDefaultConnection The method can be like this
$this->app->resolving('setDefaultConnection',function ($db,$app){
$db->setDefaultConnection('ConnectionName');
});
go back to A more practical example , Initialize a data in the controller
<?php
namespace App\Http\Controllers;
class Nut
{
public $express;
public $data;
public function __construct(\App\Contracts\Express $express)
{
$this->express = $express;
}
public function get()
{
return $this->data;
}
}
We added a new one to the original one data attribute , And the output .
At the service provider, you can This data It's initialized
Container events
$this->app->resolving('setDefaultConnection',function ($db,$app){
$db->setDefaultConnection('ConnectionName');
});
Container inheritance
The way of inheritance and The event is a bit like , But you need to return the object
$this->app->extend('App\Http\Controllers\Nut', function ($service, $app) {
$service->data = 'hello extend';
return $service;
});
git add .
git commit -m Container inheritance and events
git push
边栏推荐
- 性能对比|FASS iSCSI vs NVMe/TCP
- Fantom (FTM) surged 45% before the FOMC meeting
- 与开源项目同步开发& CodeReview & Pull Request & fork怎么拉取原始仓库
- “山东大学移动互联网开发技术教学网站建设”项目实训日志二
- 新手入门:手把手从PHP环境到ThinkPHP6框架下载
- QT setting background image method
- How to survive in the bear market of encryption market?
- 农村品牌建设给年轻人的一些机会
- Set the background color of a cell in the table
- Move protocol global health declaration, carry out the health campaign to the end
猜你喜欢

马斯克推崇的柏拉图式元宇宙,PlatoFarm早已验证出答案

QT layout management -- Part stretch principle and sizepolicy

Laravel Swagger添加访问密码
![[typescript] type reduction (including type protection) and type predicate in typescript](/img/74/52fe769ed3850e01d97cb9fefb7373.png)
[typescript] type reduction (including type protection) and type predicate in typescript

Starfish OS:以现实为纽带,打造元宇宙新范式

Qframe class learning notes

DAY15(DAY16拓展):文件包含漏洞
![[typescript] learn typescript object types in depth](/img/87/a9fd2f177331863e06fcf14559eeed.png)
[typescript] learn typescript object types in depth

“山东大学移动互联网开发技术教学网站建设”项目实训日志五

“山东大学移动互联网开发技术教学网站建设”项目实训日志二
随机推荐
Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
Wechat applet - component parameter transmission, state management
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
Character type conversion
Move protocol global health declaration, carry out the health campaign to the end
Laravel服务容器(上下文绑定的运用)
Win10 compiles ffmpeg (including ffplay)
js简单代码判断打开页面的设备是电脑PC端或手机H5端或微信端
Dao race track is booming. What are the advantages of m-dao?
第五空间智能安全⼤赛真题----------PNG图⽚转换器
Qt设置背景图片方法
Qtcreator+cmake compiler settings
HCIA-R&S自用笔记(25)NAT技术背景、NAT类型及配置
7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润
Relationship between link and @import
Read and understand move2earn project - move
Countdown of the uniapp component (such as the countdown to reading the agreement and the countdown to completing learning)
Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
[typescript] learn typescript object types in depth
Basic concepts of MySQL + database system structure + extended application + basic command learning