laravel8配置全局公共函数步骤详解

1.首先添加文件,app/Helpers.php ,我这里是这个名字因为习惯了,你也可以自己定义

<?php
if( !function_exists("getFileName") ){
    /**
     * 从路径中获取文件名
     * @param $fileName
     * @return string
     */
    function getFileName($fileName)
    {
        $suffix = substr(strrchr($fileName,'.'),0);
        return basename($fileName, $suffix);
    }
}

if( !function_exists("getFileExt") ){
    /**
     * 从路径中获取文件扩展名
     * @param $fileName
     * @return string
     */
    function getFileExt($fileName)
    {
        $str = strrev($fileName);
        return strrev(strchr($str,'.',true));
    }
}

if( !function_exists("getFileInfo") ){
    /**
     * 从路径中获取文件扩展名
     * @param $fileName
     * @return string
     */
    function getFileInfo($fileName, $type)
    {
        if($type == PATHINFO_EXTENSION){
            return getFileExt($fileName);
        }else if($type == PATHINFO_FILENAME){
            return getFileName($fileName);
        }
    }
}

  2.修改根目录下composer.json 作自动加载刚刚的文件

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/helpers.php"
    ]
},

3.修改完成后记得运行 composer dumpautoload  确保让修改生效

composer dumpautoload

 

D:\company\workspace\product-nobel>composer dumpautoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: intervention/image
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: maatwebsite/excel
Discovered Package: mews/captcha
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: overtrue/laravel-wechat
Package manifest generated successfully.
Generated optimized autoload files containing 34468 classes

  

posted on 2023-06-30 18:15  andydaopeng  阅读(397)  评论(0编辑  收藏  举报

导航