ChatGPT解决这个技术问题 Extra ChatGPT

A non-empty PSR-4 prefix must end with a namespace separator

I'm trying to setup PSR-4 with Composer but I'm just getting A non-empty PSR-4 prefix must end with a namespace separator.

My autoload in my composer.json looks like this:

"autoload": {
    "psr-4": {
        "Acme\\models" : "app/models"
    }
},

app/models is empty.

What am I doing wrong? How can I fix this?


M
Marwelln

Someone made a comment but removed it. He mentioned I was missing \\ at the end of Acme\\models. Acme\\models\\ will get rid of the message and work as it should.


Yes, PSR-4 requires the trailing slash. (well double since it needs to be escaped)
I must say this is not only a stupid requirement, the spec also makes it extremely clear that this is the case. Hence the reason I show up here for a very basic issue.
After using this answer, I also found that composer validate also gives you a good indication of what's wrong.
this cause issues when using repository type path, relative, symlinked, as it seems it tries to somehow recreate symlink just after this operation and has obsolete or cached composer.json
I did this and still got the same error, should I remove the vendor or clear some cache?
M
M at

As others said PSR-4 requires the trailing slash

Though I had to convert / to \\ in Windows (should work fine on Linux):

    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now