PSR-4 autoloading not working

SUB0DH picture SUB0DH · Jun 30, 2014 · Viewed 12.2k times · Source

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

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

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

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

The file BackendController.php has the namespace Modules\ModuleName\Controllers.

And in routes.php, I have the following:

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

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

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 vendor directory, to the server.

Answer

Unnawut picture Unnawut · Jun 30, 2014

From PSR-4 specification:

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

So you'll need to rename your modules and controllers folders to Modules and Controllers respectively.

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.