106

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?

Marwelln
  • 28,492
  • 21
  • 93
  • 117

3 Answers3

212

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.

Marwelln
  • 28,492
  • 21
  • 93
  • 117
  • 7
    Yes, `PSR-4` requires the trailing slash. (well double since it needs to be escaped) – Connor Tumbleson Jan 30 '14 at 17:43
  • 3
    I must say this is not only a stupid requirement, the [spec](http://www.php-fig.org/psr/psr-4/) also makes it extremely clear that this is the case. Hence the reason I show up here for a very basic issue. – aross Apr 24 '14 at 14:21
  • 23
    After using this answer, I also found that `composer validate` also gives you a good indication of what's wrong. – dave Aug 30 '15 at 06:35
  • 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 – FantomX1 Jun 28 '20 at 21:14
  • I did this and still got the same error, should I remove the vendor or clear some cache? – shamaseen Sep 27 '21 at 22:54
  • For anyone using **symlinked**, you should delete the symlinked folder before trying again. – shamaseen Sep 27 '21 at 22:56
12

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/"
        }
    },
M at
  • 1,020
  • 1
  • 12
  • 26
0

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

Md Shayon
  • 79
  • 5