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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31917950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:42:57  来源:igfitidea点击:

How can I autoload a custom class in Laravel 5.1?

phplaravelcomposer-phpautoload

提问by GerBawn

I've created a libraryfolder within the appfolder to add my own classes.

我在library文件夹中创建了一个文件app夹来添加我自己的类。

enter image description here

在此处输入图片说明

This is the content of the file app/library/helper.php:

这是文件的内容app/library/helper.php

<?php

namespace Library;

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

I added the namespace to composer.json:

我将命名空间添加到composer.json

enter image description here

在此处输入图片说明

and then I ran

然后我跑了

$ composer dump-autoload

but it does not seem to have any effects.

但它似乎没有任何影响。

The files

文件

  • vendor/composer/autoload_psr4.php
  • vendor/composer/autoload_classmap.php
  • vendor/composer/autoload_psr4.php
  • vendor/composer/autoload_classmap.php

did not change.

没有改变。

If I try to create an instance of MyHelper, Laravel reports the following error:

如果我尝试创建 的实例MyHelper,Laravel 会报告以下错误:

enter image description here

在此处输入图片说明

I'm not sure what I am doing wrong.

我不确定我做错了什么。

回答by Maxim Lanin

Use filesdirective in composer.json: https://getcomposer.org/doc/04-schema.md#files

使用files指令composer.jsonhttps: //getcomposer.org/doc/04-schema.md#files

{
    "autoload": {
        "files": ["app/library/helper.php"]
    }
}

回答by localheinz

Your autoloading configuration is almost good, but you have

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

  • got the namespaces wrong
  • got the paths wrong
  • 命名空间错了
  • 路径错误

To fix the problem, adjust your autoloading configuration:

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

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

Then rename the directory /libraryto /Library(note the case).

然后将目录重命名/library/Library(注意大小写)。

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

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

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

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

namespace App\Library;

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

For reference, see:

参考:

回答by Rutvik Bhatt

Use composer.json :

使用 composer.json :

   "autoload": {
    "classmap": [
        "database",
        "app/Transformers"
    ]
 },

Add your auto load directories like I added app/Transformers.

添加您的自动加载目录,就像我添加的 app/Transformers 一样。

Don't Forget to add run composer dump-autoload.

不要忘记添加 run composer dump-autoload

The only problem with this method is you need to run composer dump-autoloadwhenever you add new class do that directory.

此方法的唯一问题是composer dump-autoload每当您添加新类时都需要运行该目录。

Or You can use "Files" in composer.json.

或者您可以在 composer.json 中使用“文件”。

"autoload": {
    "files": ["src/MyLibrary/functions.php"]
}

回答by Zachary Weixelbaum

I know this question was answered a while ago, but the reason it isn't working is that you need to give the namespace that corresponds with the file structure. Therefore, since the Library class is inside the App folder you need:

我知道这个问题已经在不久前得到了回答,但它不起作用的原因是您需要提供与文件结构相对应的命名空间。因此,由于 Library 类位于 App 文件夹中,因此您需要:

namespace App\Library;

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

Additionally, if you are going to call the class MyHelper, you need to call the file MyHelper.php

此外,如果您要调用 class MyHelper,则需要调用文件 MyHelper.php