[英]Class file not found by composer and psr-4
I am having very hard times understanding how to use autoloading by psr-4
.我很难理解如何通过
psr-4
使用自动加载。
After loading vagrant and setting and testing all variables in Homestead.yaml
I have prepared a file structure as the following:在加载 vagrant 并在
Homestead.yaml
设置和测试所有变量后,我准备了一个文件结构如下:
\app
\folder
-- test.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
and the following are my codes:以下是我的代码:
index.php索引.php
<?PHP
namespace app;
require('vendor/autoload.php');
$object = new folder\test();
composer.json作曲家.json
"autoload":{
"psr-4":{
"app\\": "app"
}
}
test.php测试文件
<?php
namespace app\folder;
class test
{
function __construct ()
{
echo 'construction done right.';
}
}
But, after trying to visit the page, these are the error message displayed on the page:但是,在尝试访问该页面后,这些是页面上显示的错误消息:
(!) Fatal error: Uncaught Error: Class 'app\\folder\\test' not found in /home/vagrant/web/sites/app/index.php on line 6
(!) 致命错误:未捕获的错误:在第 6 行的 /home/vagrant/web/sites/app/index.php 中找不到 Class 'app\\folder\\test'
( ! ) Error: Class 'app\\folder\\test' not found in /home/vagrant/web/sites/app/index.php on line 6( ! ) 错误:在第 6 行的 /home/vagrant/web/sites/app/index.php 中找不到“app\\folder\\test”类
Would you help me understand and fix this error?你能帮我理解并修复这个错误吗?
To fix it for your current setup, use the following:要针对您当前的设置修复它,请使用以下命令:
"autoload":{
"psr-4":{
"app\\": ""
}
}
Your composer.json
is in the app-directory, so there's no subdirectory named app to reference.您的
composer.json
位于 app 目录中,因此没有名为 app 的子目录可供引用。
I would actually recommend to change your directory structure to the following:我实际上建议将您的目录结构更改为以下内容:
\app
\src
\folder
-- test.php
-- index.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
And then in composer.json, set the following:然后在 composer.json 中,设置以下内容:
"autoload":{
"psr-4":{
"app\\": "src"
}
}
This makes sure that all files belonging to your 'app' namespace are contained within a single subdirectory.这可确保属于您的“app”命名空间的所有文件都包含在单个子目录中。
Finally, I would recommend you to use a vendor namespace to prevent conflicts, and to use the naming guidelines from PSR-2.最后,我建议您使用供应商命名空间来防止冲突,并使用 PSR-2 中的命名指南。
It doesn't work because you have told Composer that the classes from the app
namespace are in the app
subdirectory but there is no app
subdirectory.它不起作用,因为您已经告诉 Composer 来自
app
命名空间的类在app
子目录中,但没有app
子目录。
The entire application is stored in the app
directory and it's name doesn't really matter for the application.整个应用程序存储在
app
目录中,它的名称对应用程序来说并不重要。 The classes of the app
namespace are stored in the current directory and the sub-namespaces are stored in subdirectories with the same name. app
命名空间的类存储在当前目录中,子命名空间存储在同名的子目录中。
Accordingly, your composer.json
file should read:因此,您的
composer.json
文件应为:
"autoload": {
"psr-4": {
"app\\": ""
}
}
Or, to be more clear, you can put .
或者,更清楚地说,您可以将
.
(the current directory) as the location of the app\\
namespace: (当前目录)作为
app\\
命名空间的位置:
"autoload": {
"psr-4": {
"app\\": "."
}
}
After you make the change run composer dump-autoloader
in the main application directory and it will start working.进行更改后,在主应用程序目录中运行
composer dump-autoloader
它将开始工作。
I think you won't need PSR-4 just add classmap
我认为您不需要 PSR-4 只需添加
classmap
classmap
parameter for example : classmap
参数例如:
"autoload": {
"classmap": [
"app"
]
}
After adding this run composer dump-autoload
and you should see a number of classes being added.添加此运行
composer dump-autoload
,您应该会看到添加了许多类。
Hope this helps.希望这会有所帮助。
in composer.json try to set在 composer.json 中尝试设置
"autoload":{
"psr-4":{
"": "src/"
}
}
then do execute this command然后执行此命令
composer dump-autoload -o
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.