Hyperf2.1版本自定义函数的编写流程步骤及使用
swoole  /  管理员 发布于 2年前   1378
最近学习Hyperf框架,各个模块都点点看看
今天点到自定义函数这里刚好功能需要,所以记录一下
不知道添加的是否科学,如有更好的方式麻烦留言一下
我是写laravel的听说hyperf是大量借鉴laravel,所以我首先找helpers.php看看在不在
肯定是不在的,
然后我又自己在app目录下添加了Functions.php文件
在composer.json中的autoload里面的files上也加上路径
"autoload": { "psr-4": { "App\\": "app/" }, "files": [ "app/Functions.php" ] },
编写functions.php
\app\Functions.php
<?php
declare(strict_types=1);
//自定义函数
if(! function_exists("zdy_test") ){
/**
*test
*/
function zdy_test(){
return 'test';
}
}
if(! function_exists("random_code") ){
/**
*生成随机字符串
*/
function random_code(int $num = 5){
$str="qwertyuiopasdfghjklzxcvbnmQWERTYUOPASDFGHJKZXCVBNM123456789";
$string = substr(str_shuffle($str), 0,$num) ;
return $string;
}
}
if(! function_exists("format_date") ){
/**
*根据时间戳计算与当前时间的间距及格式化单位
*/
function format_date($time){
$t = time() - $time;
$f = array('31536000'=>'年', '2592000'=>'个月', '604800'=>'星期', '86400'=>'天', '3600'=>'小时', '60'=>'分钟', '1'=>'秒');
foreach ($f as $k=>$v) {
if (0 !=$c=floor($t/(int)$k)) {
return $c.$v.'前';
}
}
}
}
让composer重建自动加载的信息 运行命令:
composer dump-autoload
果然可以
调用其中的自定义函数format_date :
因为这函数是需要在模板视图里面格式化时间的
使用方式:{{format_date($item->pubtime)}}
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号