PSR-4 自动加载不工作 [英] PSR-4 autoloading not working

查看:36
本文介绍了PSR-4 自动加载不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 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 具有命名空间 ModulesModuleNameControllers.

The file BackendController.php has the namespace ModulesModuleNameControllers.

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

And in routes.php, I have the following:

Route::resource('backend/modules/module-name', 'ModulesModuleNameControllersBackendController');

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

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

Class ModulesModuleNameControllersBackendController 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?

由于我没有访问该网络服务器的 shell,所以我没有在网络服务器上安装 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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆