grenadecx's avatar

Does not comply with psr-4 autoloading standard. Skipping.

I'm currently updating an old Laravel app with some packages from 5.7 to 8. But I've run into warnings or errors such as:

Class Rocketlabs\Helpers\App\Models\Emailvalidator located in ./vendor/rocketlabs/helpers/src/app/Models/Emailvalidator.php does not comply with psr-4 autoloading standard. Skipping.

And I'm not sure what I'm doing wrong. I have a package at gitlab called rocketlabs/helpers.

Simpliefied file structure

composer.json
src
src/app/Models/Emailvalidator.php

And inside that file src/app/Models/Emailvalidator.php, I have the following namespace:

namespace Rocketlabs\Helpers\App\Models

My composer.json have the following:

{
    "name": "rocketlabs/helpers",
    "description": "Helper package",
    "authors": [
        {
            "name": "",
            "email": ""
        }
    ],
	"autoload": {
		"psr-4": {
			"Rocketlabs\\Helpers\\": "src"
		},
	},
    "minimum-stability": "dev",
	"extra": {
		"laravel": {
			"providers": [
				"Rocketlabs\\Helpers\\HelpersServiceProvider"
			]
		}
	}
}

If a kind soul could point me in the right direction I would really appreciate it. What am I missing?

0 likes
10 replies
jlrdw's avatar

Usually updating composer takes care of that, but of course make sure any package you install, your system meets the requirements meaning PHP version Etc.

Remember some packages depend on other packages, the requirements must match.

grenadecx's avatar

Thanks. This is my own package. I run latest composer as of today, 2.0.13 with php 8.0.5.

But as I get that warning, I would assume either my structure or namespace is wrong, but I can't figure out what I need to change to get it psr-4 compliant because it looks fine to me.

jlrdw's avatar

What is Github link to this?

You did read all of upgrade guide? Each one?

grenadecx's avatar

It's hosted on a private git. But since the package is pretty stripped, I've imported it into a github repo:

https://github.com/grenadecx/helpers/tree/v2

I've read the upgrade guides but that doesn't really help with upgrading the package or about the psr-4 error/wrningI get. I would assume it only have with the composer.json, the filestructure and the namespace in the file, but what isn't adding up?

martinbean's avatar

@grenadecx It’s Composer telling you that the namespace doesn’t match the directory.

In the autoload section, you’re telling Composer that the Rocketlabs\Helpers namespace maps to the src folder, but then you’re adding an extra App segment into your namespace declaration.

Either drop App from the namespace (so it’s just Rocketlabs\Helpers\Models), or update your autoload definition to include App in the namespace:

{
    "psr-4": {
        "Rocketlabs\Helpers\App\": "src/"
    }
}
grenadecx's avatar

Alright, that still confuses me however.

The reason for me adding the extra app inside the namespace is because the file is inside app/Models folder. Isn't that how it should be?

If I have a file structure like this:

composer.json
src/Test1.php
src/app/Models/Test2.php
src/app/Events/Test3.php

If I kept

{
    "psr-4": {
        "Rocketlabs\\Helpers\\": "src/"
    }
}

What namespaces would be correct for the files above then?

I appreciate taking the time to walk me through this.

chiefguru's avatar
Level 8

@grenadecx Sometimes it's staring you in the face and you don't see it.

The capitalisation of every directory below the src/ directory must match the capitalisation of everything in the namespace and your example doesn't match.

Either make it a capital A App in the directory name or make it a lowercase a app in the namespace.

BTW: A lowercase a app in the namespace is just plain wrong.

2 likes
grenadecx's avatar

You're right, thanks. I thought I tried changing that, but maybe composer didn't care until I redownloaded the package from the vcs again.

I ended up with adding an additional

"Rocketlabs\Helpers\App\": "src/app/",

Thanks guys for explaining.

edgreenberg's avatar

All of my database seeders are failing with this error. The directories database/seeders/seedername doesn't match the namespace of Database\Seeders\SeederName. I get that. I looked at the current directory tree in https://github.com/laravel/laravel/blob/9.x/database/seeders/DatabaseSeeder.php and found that even there, in a new Laravel, the directories are lower case: database/seeders although the basic seeder DatabaseSeeder is PascalCase. So I'm, confused. Do I just have to rename the class files or do I have to rename the directories away from what Larael provides?

Please or to participate in this conversation.