app目录下创建一个名为helpers.php的文件(app/helpers.php)并编辑其内容如下
app\helpers.php
<?php
// 全局函数
/**
* 三个一组相加
*/
if (!function_exists('sgsyz')) {
function sgsyz($twowthrewNinetenw)
{
$n = 0;
$y = 0;
for ($i = 0; $i < count($twowthrewNinetenw); $i++) {
$n++;
$y++;
if ($n == 3) {
$wu[] = $twowthrewNinetenw[$i];
$wu[] = $twowthrewNinetenw[$i - 1];
$wu[] = $twowthrewNinetenw[$i - 2];
rsort($wu);
$sor = $wu[0] + $wu[1] + $wu[2];
$one[$y - 3] = $sor;
$one[$y - 2] = $sor;
$one[$y - 1] = $sor;
$wu = array();
$n = 0;
}
}
return $one;
}
}
if (! function_exists('success')) {
/**
* @param array $data
* @param string $msg
* @param int $code
*
* @return array
*/
function success($data = [], $msg = CommonConst::SUCCESS_CODE_MSG, $code = CommonConst::SUCCESS_CODE)
{
return [
'code' => $code,
'message' => $msg,
'data' => $data
];
}
}
if (! function_exists('error')) {
/**
* @param int $code
* @param string $msg
* @param array $data
*
* @return array
*/
function error($code = CommonConst::ERROR_CODE, $msg = CommonConst::ERROR_CODE_MSG, $data = [])
{
return [
'code' => $code,
'message' => $msg,
'data' => $data
];
}
}
要让应用能够正确找到 helpers.php 文件,还要修改项目根目录下 composer.json 的 autoload 配置:
composer.json
composer.json
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
},
composer dump-autoload
# 或
composer dumpautoload
应用
# 找个控制器里放函数即可因为是全局的不用调用什么的
sgsyz($twowthrewNinetenw)
return response()->json(error('4001','error'));