laravel PSR-4 自动加载不工作

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

PSR-4 autoloading not working

phplaravellaravel-4autoloadpsr-4

提问by SUB0DH

I have created an app/modulesdirectory and autoloaded it using PSR-4 like this:

我创建了一个app/modules目录并使用 PSR-4 自动加载它,如下所示:

"psr-4": {
    "Modules\": "app/modules"
}

And I also did composer dumpautoload. I have the following directory structure:

我也这样做了composer dumpautoload。我有以下目录结构:

app
- ...
- modules
-- ModuleName
--- controllers
---- BackendController.php
...

The file BackendController.phphas the namespace Modules\ModuleName\Controllers.

该文件BackendController.php具有命名空间Modules\ModuleName\Controllers

And in routes.php, I have the following:

在 中routes.php,我有以下内容:

Route::resource('backend/modules/module-name', 'Modules\ModuleName\Controllers\BackendController');

But whenever I try to access 'backend/modules/module-name', I get a ReflectionExceptionwith the following message:

但是每当我尝试访问“后端/模块/模块名称”时,我都会收到ReflectionException以下消息:

Class Modules\ModuleName\Controllers\BackendController does not exist

What may be causing the problem? When I run it in my local machine, it seems to work, but I can't get it to work on the webserver. Are there any server configuration scenarios, that may be causing this problem?

什么可能导致问题?当我在本地机器上运行它时,它似乎可以工作,但我无法让它在网络服务器上运行。是否有任何服务器配置方案可能导致此问题?

Since I don't have shell access to that webserver, I don't have composer installed on the webserver but it is installed on my local machine. I have uploaded all the files including vendordirectory, to the server.

由于我没有对该网络服务器的 shell 访问权限,因此我没有在网络服务器上安装 composer 但它安装在我的本地机器上。我已将包括vendor目录在内的所有文件上传到服务器。

回答by Unnawut

From PSR-4 specification:

PSR-4 规范

All class names MUST be referenced in a case-sensitive fashion.

所有类名都必须以区分大小写的方式引用。

So you'll need to rename your modulesand controllersfolders to Modulesand Controllersrespectively.

因此,您需要将modulescontrollers文件夹分别重命名为ModulesControllers

So it becomes:

所以就变成了:

app
- ...
- Modules
-- ModuleName
--- Controllers
---- BackendController.php
...

I wouldn't recommend renaming your namespaces to lowercase names because that just breaks the consistency in your code and project structure. It will be a headache to maintain and figure out which part of your namespace needs to be capitalized which one doesn't.

我不建议将命名空间重命名为小写名称,因为这会破坏代码和项目结构的一致性。维护和弄清楚命名空间的哪一部分需要大写,哪一部分不需要大写将是一件令人头疼的事情。

回答by Marcin Nabia?ek

You should look at capitalization.

你应该看看大小写。

Probably you test it on Windows machine so path

可能你在 Windows 机器上测试它所以路径

'Modules\ModuleName\Controllers\BackendController'

is the same as

是相同的

'modules\ModuleName\controllers\BackendController'

But on Linux they are 2 different paths. You should probably change in your routes.phpline from

但在 Linux 上,它们是 2 条不同的路径。你可能应该改变你的routes.php

Route::resource('backend/modules/module-name', 'Modules\ModuleName\Controllers\BackendController');

to

Route::resource('backend/modules/module-name', 'modules\ModuleName\controllers\BackendController');