当前位置:网站首页>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
边栏推荐
猜你喜欢

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

Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect

超简单集成HMS ML Kit 人脸检测实现可爱贴纸

Introduction to C language array to proficiency (array elaboration)

DAY4:MySQL 数据库的建立及简单实用

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

【电子电路】ADC芯片如何选型

Laravel Swagger添加访问密码

闪贷Dapp的调研及实现

新手入门:手把手从PHP环境到ThinkPHP6框架下载
随机推荐
Starfish OS: create a new paradigm of the meta universe with reality as the link
Thinkphp6管道模式Pipeline使用
Wechat applet - screen height
Laravel Swagger添加访问密码
新手入门:手把手从PHP环境到ThinkPHP6框架下载
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
Selection options of uniapp components (such as package selection)
Move protocol global health declaration, carry out the health campaign to the end
WIN10 编译ffmpeg(包含ffplay)
Masscan使用教程.
深度学习的趣味app简单优化(适合新手)
MySQL decompressed version windows installation
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration
Elastic box flex
Fantom (FTM) 价格将在未来几天飙升 20%
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
DAY14:Upload-labs 通关教程
Fvuln-自动化web漏洞检测工具
Win10 compiles ffmpeg (including ffplay)
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求