当前位置:网站首页>laravel框架中实现封装公共方法全局调用
laravel框架中实现封装公共方法全局调用
2022-07-29 02:10:00 【陈卿诺语】
1. 创建 functions.php
在 app/Helpers/下新建一个文件 functions.php,在内部补充如下代码:
<?php
/**
* 公用的方法 返回json数据,进行信息的提示
* @param $status 状态
* @param string $message 提示信息
* @param array $data 返回数据
*/
function success($msg = '',$data = array()){
$result = array(
'code' => 20000,
'msg' =>$msg,
'data' =>$data
);
exit(json_encode($result));
}
function error($msg = '',$data = array()){
$result = array(
'code' => 50000,
'msg' =>$msg,
'data' =>$data
);
exit(json_encode($result));
}
2. 配置 composer.json
打开项目根目录下的 composer.json 文件,找到"autoload" 配置项,补充如下代码:
"files":[
"app/Helper/functions.php"
]3. 执行 composer 命令
打开终端,执行下面的命令
composer dump-autoload4. 测试
public function cqny(){
return success('我口袋只剩玫瑰一片,此行山高又路远。');
return error('我口袋只剩玫瑰一片,此行山高又路远。');
}

边栏推荐
- Meeting notice of meeting OA
- MySQL基本操作和基于MySQL基本操作的综合实例项目
- Multimodal unsupervised image to image translation
- What if there is not enough time for adequate testing?
- Understand the evolution of redis architecture in one article
- 3d智能工厂工艺流转可视化交互展示应用优点
- 手把手教你安装VSCode(附带图解步骤)
- Code implementation - the greatest common factor of polynomials (linear algebra)
- 快速掌握Nodejs安装以及入门
- ES6详解 快速上手!
猜你喜欢

C language to achieve the three chess game

九宫格心形拼图小程序源码/带流量主微信小程序源码

Interprocess communication - detailed explanation of the pipeline (explanation of graphic cases)

Teach you how to install vscode by hand (with illustrated steps)

FPGA skimming memory (Verilog implementation of ram and FIFO)

Talk about 11 tips for interface performance optimization

优炫软件任命黄志军为公司总经理

Source code and display of 18 classic programs in C language vs2019

JMeter's BeanShell generates MD5 encrypted data and writes it to the database

Time pit in MySQL driver
随机推荐
Quickly master nodejs installation and getting started
ES6 event binding (v-on usage)
Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM
After 4 years of testing experience, I finally entered Alibaba. Two months later, I chose to resign naked
where、having、group by、order by,is null,not in,子查询,delete,日期函数
物联网组件
Explain asynchronous tasks in detail: task status and lifecycle management
Continuous learning / life long learning
工程经济学名词解释
FPGA skimming memory (Verilog implementation of ram and FIFO)
远离才会思念
全新UI四方聚合支付系统源码/新增USDT提现/最新更新安全升级修复XSS漏洞补单漏洞
In depth analysis - Pretreatment
TCP重传机制有哪些?
Explanation of engineering economics terms
Time pit in MySQL driver
Where, having, group by, order by, is null, not in, subquery, delete, date function
工程经济学知识点总结
以科技传递温度,vivo亮相数字中国建设峰会
HTTP断点续传以及缓存问题