简体   繁体   中英

namespace defined in autoload psr-4 using in laravel 5 app.php giving not found error

stucked my heads in some errors.i dont getting where i have done my mistake. I am using laravel 5 and installed it.I want to use l5-repository so i installed https://github.com/prettus/l5-repository this repository using composer commnad:

composer require prettus/l5-repository

and i made all changes as per installation document and its working fine.

after installing repository using composer my directory structure is as below:

curovis
|-- composer.json
|-- composer.lock
|-- app
|-- bootstarp
|-- config
|-- database
`-- vendor
    |-- composer
    `-- prettus
        `-- l5-repository
            |-- src
            |   `-- Prettus
            |       `-- Repository
            `-- composer.json

after this as per doc i have made following entry in /var/www/curovis/config/app.php : Prettus\\Repository\\Providers\\RepositoryServiceProvider::class, and its working fine. Now i want to change composer.json of root directory entry as following:

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Prettus\\Repository\\": "vendor/prettus/l5-repository/src/Prettus"
        }
    },

and use composer update command. it also works fine. now i want use same repo with another name so i have change composer.json with follwing:

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "RepoTest\\Repository\\": "vendor/repotest/l5-repository/src/RepoTest"
        }
    },

and add RepoTest\\Repository\\Providers\\RepositoryServiceProvider::class, in app.php file.run composer update command. then it gives following error:

FatalErrorException in /var/www/curovis/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php line 146: Class 'RepoTest\Repository\Providers\RepositoryServiceProvider' not found

i cant understand why laravel is looking for /var/www/curovis/vendor/laravel/framework/src this path instead of provided as "RepoTest\\\\Repository\\\\": "vendor/repotest/l5-repository/src/RepoTest" in composer.json. is anything i am missing or any error in composer. Thanks for help.

You NEVER add autoloading for the packages you added inside your main composer.json . The path "vendor" should never appear there.

I recognize you are trying to add a package, then modify it and use that instead. You changed the autoloading prefix from "Prettus" to "RepoTest", but did you also change the namespace in the PHP files? Simply renaming the path does not affect the PHP class names and namespaces, so if you rename a file, and inside that file there is no matching class defined, autoloading will fail.

Whatever it is you are trying to do, I think it is a good idea to ask about that instead of asking to fix problems you think are necessary because of the way you do solve your original problem. If you want to know how to modify an existing project and use your variant of it: Ask about it.

sovled above error by changing composer entry: when i have see autoload_classmap.php and autoload_psr4.php files of /vendor/composer/ folder autoload_classmap.php file does not contain namespace that i require. so i have made following change in my composer.json :

"autoload": {
        "classmap": [
            "database","vendor/repotest/src/Repotest/Repository/"
        ],
        "psr-4": {
            "App\\": "app/",
            "Repotest\\Repository\\": "vendor/repotest/src/Repotest/Repository/"
        }
    },

so by making entry in "classmap": make entry in autoload_classmap.php and working fine now. Thanks @sven for your help.

Example:

"autoload": {
    "classmap": [
        "database"
    ],
    "files": [
        "app/helper.php"
    ],
    "psr-4": {
        "App\\": "app/"
    }
}

Default Composer file to load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM