Reputation: 2933
I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
Fatal error: Uncaught Error: Class 'App\Init' not found in C:\wamp64\www\activity\Public\index.php on line 5
|MainFolder
|App
|Public
|Vendor
|ACT
|composer
|autoload.php
|composer.json
{
"name": "vendor/activity",
"description": "descrip",
"require": {
"php": ">=5.6.25"
},
"authors":[
{
"name": "John Doe",
"email": "[email protected]"
}
],
"autoload":{
"psr-4": {
"ACT\\": "vendor/",
"App\\": "/"
}
},
"config":{
"bin-dir": "bin"
}
}
<?php
namespace App;
class Init
{
public function __construct()
{
echo "Loaded!!";
}
}
<?php
require_once '../vendor/autoload.php';
$init = new \App\Init;
<?php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
"ACT" => array($vendorDir . false),
"App" => array($baseDir . '/'),
);
Obs: Already did composer dump-autoload
Upvotes: 4
Views: 10017
Reputation: 32272
/vendor
./vendor
in autoload
, the packages should all have their own fully-functionaly autoloaders that composer will find and use.
"autoload":{
"psr-4": {
"App\\": "App/"
}
},
Think of it like telling composer "look for things starting with the namespace foo\bar\
in the following folder".
Note: The folder name doesn't have to match the namespace.
Eg: Following the suggested Vendor\Package\
scheme for PSR/Composer
{
"autoload": {
"psr-4": {
"sammitch\\meatstacker\\": "src/"
}
}
}
And then:
\sammitch\meatstacker\Client
maps to src/Client.php
\sammitch\meatstacker\Bread\Rye
maps to src/Bread/Rye.php
Upvotes: 6