要求(供应商/autoload.php):无法打开流

2022-01-21 00:00:00 email php phpmailer composer-php

我知道这个问题已经发布过很多次了,但对我来说这似乎是一个不同的问题.

I know that this issue has been posted many times, but for me it seems to be a different problem.

确实是这个错误

警告:require(vendor/autoload.php):打开流失败:第 3 行的 C:xampphtdocssite_websend_mail.php 中没有这样的文件或目录

Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in C:xampphtdocssite_websend_mail.php on line 3

致命错误:require(): Failed opening required 'vendor/autoload.php' (include_path='C:xamppphpPEAR') in C:xampphtdocssite_websend_mail.php on line 3

Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:xamppphpPEAR') in C:xampphtdocssite_websend_mail.php on line 3

出现在我的代码开头的这一行:

appears at the begining of my code from this line:

require 'vendor/autoload.php';

所以,我想我的电脑中一定有一个/vendor/autoload.php 文件(我已经安装了 composer 并运行了 composer require phpmailer/phpmailer).

So, I guess there must be a /vendor/autoload.php file somewhere in my computer (I have installed composer and ran composer require phpmailer/phpmailer).

因此,我在 Windows 命令行中使用:dir/s autoload.php 查找此文件,并在此处找到:C:WindowsSysWOW64vendorautoload.php,

So, I looked for this file using: dir /s autoload.php in the Windows command line, and found one here: C:WindowsSysWOW64vendorautoload.php,

但对我来说,syswow64 文件夹与 autoload.php 没什么可看的,我看不出我在这里缺少什么.

but for me, syswow64 folder has nothing to see with autoload.php, I don't see what I am missing here.

推荐答案

您缺少的是运行 composer install,它将导入您的包并创建供应商文件夹以及自动加载脚本.

What you're missing is running composer install, which will import your packages and create the vendor folder, along with the autoload script.

确保您的相对路径正确.例如,PHPMailer 中的示例脚本位于项目根目录下的 examples/ 中,因此从那里加载 composer 自动加载器的正确相对路径是 ../vendor/autoload.php.

Make sure your relative path is correct. For example the example scripts in PHPMailer are in examples/, below the project root, so the correct relative path to load the composer autoloader from there would be ../vendor/autoload.php.

您在 C:WindowsSysWOW64vendorautoload.php 中找到的 autoload.php 可能是一个全局 composer 安装 - 您通常会在其中放置 phpcs、phpunit、phpmd 等内容.

The autoload.php you found in C:WindowsSysWOW64vendorautoload.php is probably a global composer installation – where you'll usually put things like phpcs, phpunit, phpmd etc.

composer update 不是相同的东西,可能不是你想要使用的东西.如果你的代码是用你当前的包版本测试的,那么运行 update 可能会导致损坏,这可能需要进一步的工作和测试,所以除非你有特定的原因,否则不要运行 update并准确理解它的含义.进一步澄清 - 你应该只在本地运行 composer update,永远不要在你的服务器上运行,因为它很可能会破坏生产中的应用程序.

composer update is not the same thing, and probably not what you want to use. If your code is tested with your current package versions then running update may cause breakages which may require further work and testing, so don't run update unless you have a specific reason to and understand exactly what it means. To clarify further – you should probably only ever run composer update locally, never on your server as it is reasonably likely to break apps in production.

我经常看到人们抱怨说人们不能使用 composer,因为他们不能在他们的服务器上运行它(例如,因为它是共享的并且他们没有 shell 访问权限).在这种情况下,您可以仍然使用 composer:在本地运行它(没有此类限制的环境),然后上传它生成的本地供应商文件夹以及所有其他 PHP 脚本.

I often see complaints that people can't use composer because they can't run it on their server (e.g. because it's shared and they have no shell access). In that case, you can still use composer: run it locally (an environment that has no such restrictions), and upload the local vendor folder it generates along with all your other PHP scripts.

运行 composer update 也执行 composer install,如果您当前没有 vendor 文件夹(如果你有一个新的项目签出是正常的),那么它会创建一个,并覆盖你已经拥有的任何 composer.lock 文件,更新其中标记的包版本,这就是有潜在危险.

Running composer update also performs a composer install, and if you do not currently have a vendor folder (normal if you have a fresh checkout of a project), then it will create one, and also overwrite any composer.lock file you already have, updating package versions tagged in it, and this is what is potentially dangerous.

同样,如果您当前没有 composer.lock 文件(例如,如果它没有提交到项目中),那么 composer install 也会有效地执行 <代码>作曲家更新.因此,了解两者之间的区别至关重要,因为它们绝对不可互换.

Similarly, if you do not currently have a composer.lock file (e.g. if it was not committed to the project), then composer install also effectively performs a composer update. It's thus vital to understand the difference between the two as they are definitely not interchangeable.

也可以通过命名来更新单个包,例如:

It is also possible to update a single package by naming it, for example:

composer update ramsey/uuid

这将重新解析您的 composer.json 中指定的版本并将其安装在您的供应商文件夹中,并更新您的 composer.lock 文件以匹配.如果您只需要对一个包进行特定更新,那么这比一般 composer 更新 引起问题的可能性要小得多.

This will re-resolve the version specified in your composer.json and install it in your vendor folder, and update your composer.lock file to match. This is far less likely to cause problems than a general composer update if you just need a specific update to one package.

库不包含自己的composer.lock文件是正常的;由应用程序来修复版本,而不是它们使用的库.因此,库开发人员需要保持与比应用程序开发人员需要的更广泛的主机环境的兼容性.例如,一个库可能与 Laravel 5、6、7 和 8 兼容,但使用它的应用可能出于其他原因需要 Laravel 8.

It is normal for libraries to not include a composer.lock file of their own; it's up to apps to fix versions, not the libraries they use. As a result, library developers are expected to maintain compatibility with a wider range of host environments than app developers need to. For example, a library might be compatible with Laravel 5, 6, 7, and 8, but an app using it might require Laravel 8 for other reasons.

Composer 2.0 删除了安装和更新结果之间的任何剩余不一致;如果您正在运行 composer 1.x,您绝对应该升级.

Composer 2.0 removed any remaining inconsistencies between install and update results; if you're running composer 1.x you should definitely upgrade.

相关文章