非空的 PSR-4前缀必须以名称空间分隔符结束

我正在尝试设置 PSR-4与作曲家,但我只是得到 A non-empty PSR-4 prefix must end with a namespace separator.

我的 composer.json中的 autoload是这样的:

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

app/models是空的。

我做错了什么? 我该怎么补救?

43147 次浏览

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.

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/"
}
},

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

  • Method-1

Incorrect ⬇️⬇️⬇️

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

Correct ⬇️⬇️⬇️

"autoload": {
"psr-4": {
"Acme\\models" : "app/models/"
}
},
  • Method-2: If this doesn't work try deleting vendor + composer.lock and reinstall dependencies
  • Method-3: Delete the autoload_psr4.php file in the libraries folder - it probably was created before the update and it had issues before.

Know more about PSR-4: Autoloader