Class Foo\Bar\Baz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping
This can happen for a variety of reasons.
The important thing is to pay attention to the error message which generally points very accurately to the source of the issue.
Path Case
The most common reason for this is that, as shown in the error message, the case for the different components of the pathname for Bar.php
do not match with the case for the fully qualified class name;
foo/bar/Baz.php
does not match App\Bar\Baz
.
Simply update your application or package so that each path component matches the case of its the namespace it holds:
Foo\Bar\Baz.php
File name and Class name or Namespace differences
Check the pathname against the namespace very carefully. Sometimes your named your class (or your namespace) FooBar
, but its path on disk is "foo-bar", for example. Or simply for any reason your namespace does not fully match the pathname of the files.
This will trigger a notice/warning as well. You need to either rename the files or rename the classes (or namespaces).
Usually, changing the path or files is much easier, since changing the class or namespace names would require you refactor code to match the new names, whereas changing the paths will not need you to refactor anything.
Nested namespaces and missing declaration
Let's say that you have:
"autoload": {
"psr-4": {
"Fizz\\Buzz\\": "src/"
}
},
And the class Dummy
, defined inside src/Buzz
:
// src/Buzz/Dummy.php
namespace Fizz\Buzz
class Dummy {}
The above will work, but will throw the notice like the others. The correct way would be:
// src/Buzz/Dummy.php
namespace Fizz\Buzz\Buzz
class Dummy {}
You'll need not only to make the change on the affected class, but on any other file where this class is used or imported. (e.g. by now declaring use Fizz\Buzz\Buzz\Dummy;
).
Error with autoloading. Class does not comply with psr-4 autoloading standard. Skipping
I rename all folders like this:
modules
-> ModulesNema
-> Controllers
-> Services
-> Dark.php
-> Rent.php
-> TwigExtensions
-> NemaTwigExtensions.php
-> Module.php
-> Module.php
Then change all namespace like
use Modules\ModuleNema\TwigExtensions\NemaTwigExtensions;
use Modules\ModuleNema\Services\Dark;
use Modules\ModuleNema\Services\Rent;
on composer:
"autoload": {
"psr-4": {
"Modules\\": "modules/",
"ModuleNema\\": "modules/ModuleNema"
},
},
and run
composer dump-autoload -o
now all it's working with composer 2
Composer PSR-4 autoload is not working after trying multiple methods and tutorials
While testing the given code, the problem was resolved by renaming index.php
within src
to App.php
which relates to class name in the file. Also used "xenframe\\hello\\": "src/"
in psr-4
section in composer.json
.
Also its worth mentioning that while composer was unable to autoload required class, due to PSR-4 non compliance, no error was reported by composer.
The problems were:
- The filename of class was not properly compliant with PSR-4 specifications. From section 2.3.3
The terminating class name corresponds to a file name ending in
.php
. The file name MUST match the case of the terminating class name.
- The
namespace
mentioned inpsr-4
section incomposer.json
was not matching thenamespace
used in class file. From Composer's PSR-4 Schema
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.
psr-4 autoloading issue in Laravel
The namespace is wrong in your model, you're missing the sub-namespace of Model
and have only the vendor namespace, which in the Laravel framework is set to App
, pointing to the app
folder as its base.
Changing your Admin
models namespace to include the sub-namespace will fix your issue.
namespace App\Models;
class Admin {}
This is because PSR-4 works off of file paths, with sub-namespaces being directories to travel through to reach the destined class and those directories must match the cases of the sub-namespaces. The same goes for class names, the file must match that of the class, for instance.
IF your base directory is /src
, linked to the vendor namespace Mitra, a class in the root folder of /src
will only have the namespace Mitra
. IF you have a folder in the root; /src/Models
THEN the namespace would be Mitra\Models
.
The specification for PSR-4 is a rather short specification and is very well documented, I recommend reading it if you're having trouble understanding.
Autoloading of classes through composer does not work
App.php are inside /core directory :
autoload": {
"psr-4": {
"App\\Controllers\\": "app/controllers",
"Xuborx\\Cms\\": "vendor/xuborx/cms/core"
}
}
Related Topics
How to Loop Through a MySQL Result Set
Simple Postgresql Statement - Column Name Does Not Exists
Can HTML Be Embedded Inside PHP "If" Statement
How to Validate Array in Laravel
PHP Replacing Multiple Spaces With a Single Space
Why Is PHP Session_Destroy() Not Working
Change Product Prices Via a Hook in Woocommerce 3+
Why Is Json_Encode Adding Backslashes
How to Get the User'S Local Time Instead of the Server'S Time
Replace a Whole Line Where a Particular Word Is Found in a Text File
How to Show Ajax Requests in Url
How to Get Const'S Defined on a PHP Class
Why Does PHP 5.2+ Disallow Abstract Static Class Methods
MySQL: Select Random Entry, But Weight Towards Certain Entries