Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR-4 autoloading not working

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.

like image 792
SUB0DH Avatar asked Jun 30 '14 17:06

SUB0DH


2 Answers

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.

like image 185
Unnawut Avatar answered Nov 05 '22 09:11

Unnawut


You should look at capitalization.

Probably you test it on Windows machine so path

'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.php line from

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

to

Route::resource('backend/modules/module-name', 'modules\ModuleName\controllers\BackendController');
like image 45
Marcin Nabiałek Avatar answered Nov 05 '22 11:11

Marcin Nabiałek