Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure composer to use class names with prefixes

I have a folder structure like this:

Root-Folder/backend/user/class-user.php
Root-Folder/backend/profile/class-profile.php
Root-Folder/backend/login.php

Sample class-user.php content:

namespace myapp\user;
class User{
....
}

In my composer.json:

...
"autoload": {
        "psr-4": {
            "myapp\\": "backend/"
        }
    },
...

But this way the files are not included because they have a class- prefix in the file name. If I change the file names to user.php and profile.php, they work.

How can I configure composer to include class-user.php too?

like image 244
user83283925 Avatar asked Oct 31 '25 22:10

user83283925


1 Answers

PSR-4 requires classes names to be the same as file name that contains it. In this case you need to use "classmap" directive instead of PSR-4

https://getcomposer.org/doc/04-schema.md#classmap

{
    "autoload": {
        "classmap": ["backend"]
    }
}
like image 123
Sebas Rossi Avatar answered Nov 03 '25 11:11

Sebas Rossi