Laravel psr-4 不自动加载

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

Laravel psr-4 not autoloading

laravellaravel-4composer-phppsr-4

提问by Heather Gaye

I have a Laravel project that works fine locally (Mavericks), but classes under psr-4 aren't loading on our stage server (CentOS). I'm getting a Reflection "class not found" error every time I try composer update, or run an artisan command.

我有一个在本地运行良好的 Laravel 项目(Mavericks),但是 psr-4 下的类没有加载到我们的舞台服务器(CentOS)上。每次尝试 composer update 或运行 artisan 命令时,我都会收到一个反射“找不到类”错误。

All my app-specific classes are stored in my Laravel project under app/heatherland, eg:

我所有特定于应用程序的类都存储在我的 Laravel 项目中的 app/heatherland 下,例如:

app/heatherland/import/ImportJob.php (file contains HeatherLand\Import\ImportJob)

My composer.json contains this entry:

我的 composer.json 包含这个条目:

"autoload": {
    "classmap": [
        "app/commands",
        ...
        "app/database/seeds",
    ],
    "psr-4": {
        "HeatherLand\": "app/heatherland"
    }
},

Locally, the psr-4 classes are added to the array in vendor/composer/autoload_classmap.php. They're not added to this file on the stage server. If I cut and paste them manually, artisan commands work properly, but the next time I run a composer command, the autoload file is overwritten. The autoload_psr4.php on both local and stage has the following entry, which looks fine to me:

在本地,psr-4 类被添加到 vendor/composer/autoload_classmap.php 中的数组中。它们不会添加到舞台服务器上的此文件中。如果我手动剪切和粘贴它们,工匠命令可以正常工作,但下次运行 composer 命令时,自动加载文件将被覆盖。本地和舞台上的 autoload_psr4.php 都有以下条目,对我来说看起来不错:

'HeatherLand\' => array($baseDir . '/app/heatherland'),

Here's is a list of stuff I've tried/checked:

这是我尝试过/检查过的东西的列表:

  • The case of the class, folder and file names is consistent and correct.
  • I've been using composer dump-autoload liberally, with and without the -o option
  • I can run composer update with the --no-scripts option fine, but artisan still won't run
  • Folder names are lower-case, and I've tried changing them to reflect case of the namespaces(edit: this never happened, at least not successfully).
  • I've tried removing CamelCase from my namespace (eg, changing it to Heatherland), and there aren't any underscores in any of my folder/filenames.
  • I'm running PHP 5.4.30, and composer is up-to-date. Versions are identical on my local setup and stage server. Laravel 4.1.30.
  • No duplicate composer.phar in either system
  • 类名、文件夹名和文件名的大小写一致且正确。
  • 我一直在自由地使用 composer dump-autoload,有和没有 -o 选项
  • 我可以使用 --no-scripts 选项运行 composer update 很好,但是 artisan 仍然无法运行
  • 文件夹名称是小写的,我尝试更改它们以反映命名空间的大小写(编辑:这从未发生过,至少没有成功)。
  • 我尝试从我的命名空间中删除 CamelCase(例如,将其更改为 Heatherland),并且我的任何文件夹/文件名中都没有任何下划线。
  • 我正在运行 PHP 5.4.30,并且 composer 是最新的。我的本地设置和舞台服务器上的版本是相同的。Laravel 4.1.30。
  • 两个系统中都没有重复的 composer.phar

Any new advice welcome. At this point, I'd be really happy if I've done something silly.

欢迎任何新的建议。在这一点上,如果我做了一些愚蠢的事情,我会很高兴。

回答by Heather Gaye

PSR-4 is indeed super-touchy about case, more so than Laravel itself. Full folder paths and names must be in the same case as the namespaces. The only places the cases don't need to be the same is where there's a reference in the PSR-4 section of composer.json.

PSR-4 确实对大小写非常敏感,比 Laravel 本身更敏感。完整的文件夹路径和名称必须与命名空间的大小写相同。案例不需要相同的唯一地方是在 composer.json 的 PSR-4 部分中有引用的地方。

This only becomes a problem with case-specific operating systems. I had no problems on my Mac, but CentOS refused to play.

这只会成为特定于案例的操作系统的问题。我的 Mac 没有问题,但 CentOS 拒绝播放。

Note this is different to the practice used for Laravel-specific framework across its documentation, which uses lowercase folder paths and CamelCase namespaces. This won't cause any problems on any operating system. July 2015 ETA:this info applied to Laravel 4; looks like Laravel 5 default folder structure adheres to psr-4 standards.

请注意,这与 Laravel 特定框架在其文档中使用的做法不同,后者使用小写文件夹路径和 CamelCase 命名空间。这不会在任何操作系统上造成任何问题。 2015 年 7 月 ETA:此信息适用于 Laravel 4;看起来 Laravel 5 默认文件夹结构符合 psr-4 标准。

I eventually ended up with a folder structure like:

我最终得到了一个文件夹结构,如:

app/heatherland
  Import
    ImportJob.php

namespaces eg

命名空间,例如

HeatherLand\Import

and a composer.json entry as per the original question:

以及根据原始问题的 composer.json 条目:

"autoload": {
    "classmap": [
        "app/commands",
        ...
        "app/database/seeds",
    ],
    "psr-4": {
        "HeatherLand\": "app/heatherland"
    }
},

Note to self: Remember to run composer dump-autoload.Dump early, dump often.

自我注意:记得运行 composer dump-autoload。尽早转储,经常转储。