Laravel6自定义辅助函数
添加文件
在app
目录创建Helpsers
目录,然后添加function.php
文件
在项目根目录找到composer.json
文件打开找到autoload
,在后面加上
"files": [
"app/Helpers/functions.php"
]
效果如下
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/functions.php"
]
},
常用辅助函数
在这里贴一下我自己比较常用的
生成友好时间形式
if (!function_exists('friendly_date')) {
/**
* 生成友好时间形式
*
* @param $from
*
* @return false|string
*/
function friendly_date($from): string
{
static $now = NULL;
$now == NULL && $now = time();
!is_numeric($from) && $from = strtotime($from);
$seconds = $now - $from;
$minutes = floor($seconds / 60);
$hours = floor($seconds / 3600);
$day = round((strtotime(date('Y-m-d', $now)) - strtotime(date('Y-m-d', $from))) / 86400);
if ($seconds == 0) {
return '刚刚';
}
if (($seconds >= 0) && ($seconds <= 60)) {
return "{$seconds}秒前";
}
if (($minutes >= 0) && ($minutes <= 60)) {
return "{$minutes}分钟前";
}
if (($hours >= 0) && ($hours <= 24)) {
return "{$hours}小时前";
}
if ((date('Y') - date('Y', $from)) > 0) {
return date('Y-m-d', $from);
}
switch ($day) {
case 0:
return date('今天H:i', $from);
break;
case 1:
return date('昨天H:i', $from);
break;
default:
return "{$day} 天前";
break;
}
}
}
校验是否为中国手机号码
if (!function_exists('is_mobile_number')) {
/**
* 校验是否为中国手机号码
*
* @param string $number
*
* @return bool
*/
function is_mobile_number(string $number): bool
{
return preg_match('/^(\+?0?86\-?)?((13\d|14[579]|15[^4,\D]|16[6]|19[89]|17[135678]|18\d)\d{8}|170[059]\d{7})$/', $number);
}
}
校验是否为有效的用户名
if (!function_exists('is_valid_name')) {
/**
* 校验是否为有效的用户名
*
* @param string $name
*
* @return bool
*/
function is_valid_name(string $name): bool
{
return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name);
}
}
返回可读性更好的文件尺寸
if (!function_exists('human_file_size')){
/**
* 返回可读性更好的文件尺寸
*
* @param $bytes
* @param int $decimals
*
* @return string
*/
function human_file_size($bytes, $decimals = 2): string
{
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) .@$size[$factor];
}
}
获取指定key的输入值,并过滤空值项
if (!function_exists('filter_request_params')) {
/**
* 获取指定key的输入值,并过滤空值项
*
* @param array $paramsKey
* @param \Illuminate\Http\Request $request
*
* @return array
*/
function filter_request_params(array $paramsKey, \Illuminate\Http\Request $request): array
{
foreach ($paramsKey as $key) {
if ($request->filled($key)) {
$data[$key] = $request->input($key);
}
}
return $data ?? [];
}
}
清除指定key的缓存
if (!function_exists('clear_cache')) {
/**
* 清除指定key的缓存
*
* @param array|string $key
* @param bool $is_tags
*/
function clear_cache($key, $is_tags = false)
{
if ($is_tags){
\Cache::tags($key)->flush();
}
if (is_array($key)){
foreach ($key as $k){
\Cache::forget($k);
}
} else {
\Cache::forget($key);
}
}
}
过滤二维数组中重复的数组(以数组中某个键值为判断)
if (!function_exists('array_filter_repeat')) {
/**
* 过滤二维数组中重复的数组(以数组中某个键值为判断)
*
* @param array $array
* @param $key
*
* @return array
*/
function array_filter_repeat($array, $key)
{
$i = 0;
$temp_array = [];
$key_array = [];
foreach ($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
}
未经允许禁止转载!!!!本文标题:Laravel6自定义辅助函数