1.创建包含自定义函数的php文件
2.修改composer.json的autoload配置
3.更新autoload
创建包含自定义函数的php文件
<?php // 检查是否定义,避免冲突 // Laravel根目录/app/helpers.php if(!function_exists('yearDiff')){ function yearDiff(int $year,int $month){ if(!is_int($year)||!is_int($month)){ return ''; } if($year>1900&&$year<2100){ $Y = Date("Y",time()); $M = Date("m",time()); $age=$Y-$year; if($month>$M){ $age--; } return $age; }else{ return ''; } } }
2.修改composer.json的autoload配置(自动引入文件)
"autoload": { "classmap": [ "database/seeds", "database/factories", "app/weiXinJSSDK", "app/alipay-sdk-PHP-3.3.0" ], "psr-4": { "App\": "app/" }, "files":[ "app/helpers.php" ] },
3.更新autoload
composer dump-autoload