问题
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.
回答1:
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.
回答2:
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');
来源:https://stackoverflow.com/questions/24495987/psr-4-autoloading-not-working