Laravel PSR-4无法自动加载 [英] Laravel psr-4 not autoloading

查看:1142
本文介绍了Laravel PSR-4无法自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel项目,该项目在本地可以很好地运行(Mavericks),但是psr-4下的类没有加载到我们的舞台服务器(CentOS)上.每次我尝试更新作曲家或运行工匠命令时,都会收到反射找不到类"错误.

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项目中的app/heatherland下,例如:

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

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

我的composer.json包含以下条目:

My composer.json contains this entry:

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

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

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:

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

这是我尝试过/检查过的东西的清单:

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

  • 类,文件夹和文件名的大小写一致且正确.
  • 无论是否使用-o选项,我都在自由地使用composer dump-autoload
  • 我可以使用--no-scripts选项正常运行作曲家更新,但工匠仍然无法运行
  • 文件夹名称是小写字母,,并且我尝试过更改它们以反映名称空间的大小写(这从未发生,至少没有成功).
  • 我尝试从名称空间中删除CamelCase(例如,将其更改为Heatherland),并且我的任何文件夹/文件名中都没有下划线.
  • 我正在运行PHP 5.4.30,并且composer是最新的.我的本地设置和舞台服务器上的版本相同. Laravel 4.1.30.
  • 两个系统中都没有重复的composer.phar
  • 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

欢迎任何新建议.在这一点上,如果我做了一些愚蠢的事情,我会感到非常高兴.

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

推荐答案

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

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.

这仅在特定于案例的操作系统中成为问题.我的Mac上没有问题,但CentOS拒绝播放.

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

请注意,这与Laravel特定文档中使用小写文件夹路径和CamelCase命名空间的实践不同.这不会在任何操作系统上引起任何问题. 2015年7月预计到达时间:此信息适用于Laravel 4;看起来Laravel 5默认文件夹结构符合psr-4标准.

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.

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

I eventually ended up with a folder structure like:

app/heatherland
  Import
    ImportJob.php

命名空间,例如

HeatherLand\Import

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

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

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

注意事项:请记住要运行作曲家dump-autoload.请尽早转储,经常转储.

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

这篇关于Laravel PSR-4无法自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆