PSR-4自动加载无法正常工作 [英] PSR-4 autoloading not working
问题描述
我已经创建了一个app/modules
目录,并使用PSR-4将其自动加载,如下所示:
I have created an app/modules
directory and autoloaded it using PSR-4 like this:
"psr-4": {
"Modules\\": "app/modules"
}
我也做了composer dumpautoload
.我具有以下目录结构:
And I also did composer dumpautoload
. I have the following directory structure:
app
- ...
- modules
-- ModuleName
--- controllers
---- BackendController.php
...
文件BackendController.php
具有名称空间Modules\ModuleName\Controllers
.
在routes.php
中,我有以下内容:
Route::resource('backend/modules/module-name', 'Modules\ModuleName\Controllers\BackendController');
但是,每当我尝试访问后端/模块/模块名称"时,我都会收到带有以下消息的ReflectionException
:
But whenever I try to access 'backend/modules/module-name', I get a ReflectionException
with the following message:
Class Modules\ModuleName\Controllers\BackendController does not exist
可能是什么原因引起的问题?当我在本地计算机上运行它时,它似乎可以运行,但无法在Web服务器上运行.是否有任何服务器配置方案可能会导致此问题?
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?
由于我无权访问该Web服务器,因此我没有在Web服务器上安装composer,但已将其安装在本地计算机上.我已将包括vendor
目录的所有文件上传到服务器.
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 vendor
directory, to the server.
推荐答案
来自 PSR-4规范:
必须以区分大小写的方式引用所有类名.
All class names MUST be referenced in a case-sensitive fashion.
因此,您需要将modules
和controllers
文件夹分别重命名为Modules
和Controllers
.
So you'll need to rename your modules
and controllers
folders to Modules
and Controllers
respectively.
它变成:
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.
这篇关于PSR-4自动加载无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!