且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Laravel 5.1中自动加载自定义类?

更新时间:2022-11-19 20:24:34

您的自动加载配置几乎不错,但是您拥有

Your autoloading configuration is almost good, but you have

  • 弄错了命名空间
  • 走错了路

要解决此问题,请调整您的自动加载配置:

To fix the problem, adjust your autoloading configuration:

{
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    }
}

然后将目录/library重命名为/Library(注意情况).

Then rename the directory /library to /Library (note the case).

然后将文件/app/Library/helper.php重命名为/app/Library/MyHelper.php(请注意类名应如何与文件名匹配).

Then rename the file /app/Library/helper.php to /app/Library/MyHelper.php (note how class name should match the file name).

然后调整/app/Library/MyHelper提供的类的名称空间,以匹配PSR-4前缀(以及项目的结构)以及该类的用法:

Then adjust the namespace of the class provided by /app/Library/MyHelper to match the PSR-4 prefix (and thus the structure of your project), as well as usages of the class:

namespace App\Library;

class MyHelper 
{
    public function v($arr)
    {
        var_dump($arr);
    }
}

有关参考,请参见: