为什么我的 composer 的 autoload.php 不起作用?

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

Why my autoload.php of composer doesn't work?

phpdependenciescomposer-phprespect-validation

提问by Matteo Calò

I have a project, I use Composer and i import many thing by it... i require the autoload.php in my index (the root of project) and istead Slim, Mongo, Twig work very well. But when I call a class of Respect/Validation it doens't work; if I simply use Respect/Validation the error is:

我有一个项目,我使用 Composer 并通过它导入了很多东西......我需要在我的索引(项目的根目录)中使用 autoload.php 并且 istead Slim、Mongo、Twig 工作得很好。但是当我调用一类尊重/验证时,它不起作用;如果我只是使用尊重/验证,错误是:

Class 'Respect\Validation\Validator' not found in (path of file when i need it).

Class 'Respect\Validation\Validator' not found in (path of file when i need it).

if I try to require also here the autoload.php the errors are:

如果我尝试在这里也要求 autoload.php 错误是:

**Warning**: require_once(vendor/autoload.php): failed to open stream: No such file or directory in (path of file when i need it)

**Warning**: require_once(vendor/autoload.php): failed to open stream: No such file or directory in (path of file when i need it)

**Fatal error**: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.;C:\xampp\php\PEAR') in (path of file when i need it)

**Fatal error**: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.;C:\xampp\php\PEAR') in (path of file when i need it)

采纳答案by Davide Pastore

If you are using a file (file.php) in a particular directory like this:

如果您file.php在特定目录中使用文件 ( ),如下所示:

/app/controller/validation

and your vendor directory is in the base path of the project, you need to include the relative path to vendor directory:

并且您的供应商目录位于项目的基本路径中,您需要包含供应商目录的相对路径:

../../../vendor/autoload.php

回答by ciruvan

Try using php composer dump-autoload. It may fix that problem.

尝试使用php composer dump-autoload. 它可能会解决这个问题。

回答by Abhishek Dujari

I did this. i checked the file and permissions as always but everything looks ok. Then I open autoload.php and see ... oh! we have __DIR__ofcourse.

我这样做了。我一如既往地检查了文件和权限,但一切正常。然后我打开 autoload.php 并查看...哦!我们当然有__DIR__

I was doing this

我在做这个

require '../../vendor.autoload.php';

require '../../vendor.autoload.php';

Changed to

变成

require __DIR__.'../../vendor.autoload.php';

require __DIR__.'../../vendor.autoload.php';

Not politically correct but got me by the frustrating waste of time for the moment.

上不正确,但让我感到沮丧的是暂时浪费了时间。

ofcourse there are better alternatives to __DIR__like using a BASE Path etc. But this worked for me.

当然,还有更好的替代方案,__DIR__比如使用 BASE Path 等。但这对我有用。

回答by jaip

I had the same issue with xampp. I installed composer and used:

我对 xampp 有同样的问题。我安装了 composer 并使用了:

composer create-project --stability=dev --prefer-dist yiisoft/yii2-app-basic D:\xampp\htdocs\newapp

My issue solved now.

我的问题现在解决了。

回答by jaip

I had the same problem and i solved it by using the last version of laravel, just try this code to create your project:

我遇到了同样的问题,我通过使用 Laravel 的最新版本解决了它,只需尝试使用以下代码来创建您的项目:

composer create-project laravel/laravel exampleProject --prefer-dist

composer create-project laravel/laravel exampleProject --prefer-dist

this always uses the last version.

这总是使用最后一个版本。

回答by Francesco Tanganelli

I solved simple re-run composer update from the project Root. (my case was on Magento2, but the error and the path are the same)

我从项目根解决了简单的重新运行作曲家更新。(我的情况是在Magento2上,但错误和路径是一样的)

回答by atswann

In my case i simple forgot to change my working directory before running the command composer require cboden/ratchetin cmd this fixed it cd c:\path to project\then composer require cboden/ratchet

在我的情况下,我composer require cboden/ratchet在 cmd 中运行命令之前忘记更改我的工作目录,cd c:\path to project\然后修复它composer require cboden/ratchet

回答by Sayed Abolfazl Fatemi

I solve the problem without any installor updateof composerand just by moving require_oncefrom sub class to main page.

我在没有任何installupdateof 的情况下解决了问题composer,只需require_once从子类移动到main page.

require_once 'vendor/autoload.php';