I'm quite new to autoloading and namespaces, and attempting to add namespaces to my MVC project.
So In my composer I added:
"autoload": {
"psr-0": {
"prj\\app\\": "app/",
"prj\\app\\controller\\": "app/controller/",
"prj\\app\\classes\\": "app/classes/"
}
}
And then updated composer and ran the autodump command.
I then went back to my app to try use one of these namespaces however I just get the following back:
use \app\classes\engine; // use statement I tried
Fatal error: Uncaught Error: Class 'app\classes\engine' not found in C:\inetpub\wwwroot\web\index.php:87 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\web\index.php on line 87
I'm not sure why it is unable to find the class using the namespace, here's my entire folder structure if it may be of any use:
PRJ
├───app
│ ├───classes
│ └───controller
├───web
│ └───index.php
├───vendor
│ ├───bin
│ ├───composer
│ ├───...
└───view
├───bootstrap
└───default
/app stores the logic such as controllers and classes.
/web is the web root - the index.php is the page which visitors see and also everything is handled through here.
/vendor is the composer directory where my dependencies are stored.
There are several things going wrong. First of all, you're adding duplicate namespaces in your composer.json:
The lines:
Are unnecessary as they are already covered by:
As long as the directory under
appmatches the name of the namespace you use, there is no need to define it explicitly. So you can just add:Secondly, your
usestatement seems off, you're trying:The leading slash should not be neccessary here, if you are already in the same namespace. Additionally, you're autoloading your namespaces as
prj\appand notapp, so you're missing theprjbit. It should look something like this (when this is a file inside theappfolder):Also take a look at the PSR-0 naming conventions as you don't seem to follow them. Class names and namespace folders should be capitalized, like
App\Classes\Engineinstead ofapp\classes\engine.