laravel Composer 转储自动加载,问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32664271/
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-09-14 12:22:17  来源:igfitidea点击:

Composer dump-autoload, issue

phplaravelcomposer-phphelpersartisan

提问by Ande Caleb

While Working on a project using Laravel 4 to be precise, I decided that i wanted to make my own helper file to house my custom functions.. one of which is this below...

准确地说,在使用 Laravel 4 进行项目时,我决定制作自己的帮助文件来容纳我的自定义函数......其中之一是下面的......

function pr($ar=array(), $bool=false){

   echo '<pre>';
   print_r($ar);
   echo '</pre>';

   if($bool){
      exit;
   }

}

in my composer.json file, just after the autoload: classmap , i added myne, autoload:files -arrar and included my custom file, app/helpers as illustrated below..

在我的 composer.json 文件中,就在 autoload: classmap 之后,我添加了 myne, autoload:files -arrar 并包含了我的自定义文件 app/helpers ,如下图所示..

            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],

        "others":[
            "app/helpers.php"
        ]

and i switched to my terminal window and ran the following commands

我切换到我的终端窗口并运行以下命令

composer dump-autoload -o  

but i still got errors that my pr() function was undefined... then i tried the artisan alternative... [-o ] to optimize the files

但我仍然遇到我的 pr() 函数未定义的错误...然后我尝试了 artisan 替代... [-o ] 来优化文件

php artisan dump-autoload

but still it refused to work... and then i changed the array name from

但它仍然拒绝工作......然后我将数组名称从

"others":[
            "app/helpers.php"
        ]

to

"files":[
            "app/helpers.php"
        ]

then i got the desired response, my code could now see the custom function i wrote, please i'd like to know if there is a pattern i was supposed to follow or otherwise, in my case, i mistook " files ", for " others " and i got errors, but incase, what did i miss here, all i see is just a name-string value for the array representation....

然后我得到了所需的响应,我的代码现在可以看到我编写的自定义函数,请我想知道是否有我应该遵循的模式,或者在我的情况下,我误认为“文件”为“其他人”,我遇到了错误,但是,我在这里错过了什么,我看到的只是数组表示的名称字符串值....

采纳答案by Marcin Nabia?ek

This is how composer works. In autoloadsection you need to use fileswhen you want to load some files. In my Laravel 5 project I have for example:

这就是作曲家的工作方式。在您要加载某些文件时autoload需要使用的files部分。例如,在我的 Laravel 5 项目中:

"autoload": {
    "classmap": [
        "database",
        "tests/TestCase.php"
    ],
    "psr-4": {
        "App\": "app/",
        "verify\": "verify/"
    },
    "files": [
        "app/Helpers/functions.php"
    ]
},

If you look at documentationyou will see that you need to use filesto load any extra files by autoloader.

如果您查看文档,您会发现您需要使用files自动加载器来加载任何额外的文件。

回答by Zsw

According to the official documentation

根据官方文档

Currently PSR-0 autoloading, PSR-4 autoloading, classmap generation and files includes are supported. PSR-4 is the recommended way though since it offers greater ease of use (no need to regenerate the autoloader when you add classes).

目前支持 PSR-0 自动加载、PSR-4 自动加载、类映射生成和文件包含。PSR-4 是推荐的方式,因为它提供了更大的易用性(添加类时无需重新生成自动加载器)。

So the reason that "others"did not work was because it is not supported by composer. "others"is simply meaningless, while "files"actually have a specific autoloading mechanism.

所以没用的原因"others"是composer不支持。"others"根本没有意义,而"files"实际上有一个特定的自动加载机制