Reputation: 882
i'm a newbie at composer, so just bear with me, So i have a package, which i'm loading from local folder, and while using it, i get the following error:
Fatal error: Class 'mypkg\Layer\EasyCPT' not found in C:\xampp\htdocs\testwp\app\Cpt\location.php on line 5
My Composer.json:
"repositories": [
{
"type":"vcs",
"url":"C:/xampp/htdocs/mypkg"
}
],
"require": {
"php": ">=7.0.0",
"mypkg/particles": "master"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
Package's Composer:
"minimum-stability": "dev",
"authors": [
{
"name": "Talha Abrar",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Mypkg\\": "particles/"
}
}
Psr 4:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Mypkg\\' => array($vendorDir . '/Mypkg/particles/particles'),
'App\\' => array($baseDir . '/app'),
);
how i am using it:
<?php
namespace App\Cpt;
use Mypkg\Layer\EasyCPT;
class Location extends EasyCPT{
protected $plural = 'locations';
}
Main auto-loading file:
require __DIR__.'/vendor/autoload.php';
use App\Init\EasyWP;
new EasyWP();
Upvotes: 2
Views: 2723
Reputation: 166919
You use the namespace as:
use Particles\Layer\EasyCPT;
but in autoload
section defining as:
"Mypkg\\": "particles/"
which is not consistent.
You should replace Mypkg
with the right namespace name, e.g.
"autoload": {
"psr-4": {
"Particles\\": "particles/"
}
}
So requesting Particles\Layer\EasyCPT
namespace will look for class in particles/Layer/EasyCPT.php
file.
As per Composer's PSR-4 documentation:
Under the
psr-4
key you define a mapping from namespaces to paths, relative to the package root. When autoloading a class likeFoo\\Bar\\Baz
a namespace prefixFoo\\
pointing to a directorysrc/
means that the autoloader will look for a file namedsrc/Bar/Baz.php
and include it if present. Note that as opposed to the olderPSR-0
style, the prefix (Foo\\
) is not present in the file path.
If your project doesn't follow PSR-4 approach, use classmap instead to scan for all of your classes, e.g.
"autoload": {
"classmap": ["particles/"],
"exclude-from-classmap": ["/tests/"]
}
To regenerate autoload
manually, run:
composer dump-autoload -o
and check autoload files in vendor/composer/
whether the references to classes were generated correctly.
Upvotes: 2