当前位置:网站首页>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('我口袋只剩玫瑰一片,此行山高又路远。');
}

边栏推荐
- Understanding service governance in distributed development
- PHP幸运抽奖系统带后台源码
- 详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
- ROCBOSS开源微社区轻论坛类源码
- time_ Wait and close_ Cause of wait
- 如果非要在多线程中使用 ArrayList 会发生什么?
- 一文搞懂 Redis 架构演化之路
- 2022/07/28 learning notes (day18) common APIs
- Some records during the development of ros2/ros1
- 如何把thinkphp5的项目迁移到阿里云函数计算来应对流量洪峰?
猜你喜欢

HTTP缓存
![[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages](/img/dd/c7ad9addb0d15611d996db87bf469f.png)
[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages

XSS range (II) xss.haozi

Multimodal unsupervised image to image translation

一文理解分布式开发中的服务治理

How to migrate thinkphp5 projects to Alibaba cloud function computing to cope with traffic peaks?

ES6 event binding (v-on usage)

云开发打工人必备上班摸鱼划水微信小程序源码

I want to talk about high concurrency.

ECCV 2022 | AirDet:无需微调的小样本目标检测方法
随机推荐
Intel's IPP Library (Integrated Performance Primitives)
VR safety training of mine mining virtual reality improves employees' vigilance and protection awareness
[golang learning notes 2.2] map, structure and interface
Driverless obstacle avoidance technology
How does the Devops team defend against API attacks?
fopen、_ Wfopen reads Unicode encoded files
Ordinary happiness
工程经济学名词解释
Code random notes_ Hash_ 349 intersection of two numbers
In depth analysis - Pretreatment
h. 264 code stream explanation
远离才会思念
When synchronized encounters this thing, there is a big hole, so be careful
Ten methods to prevent blackmail software from attacking data
Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
线上3d数字展厅制作方案及优点
[quality] code quality evaluation standard
Eight practical new functions of es2022
C language to achieve the three chess game
工程经济学简答题