使用laravel 5 app.php在autoload psr-4中定义的命名空间给出未找到的错误

时间:2015-07-28 11:14:15

标签: php laravel laravel-5 composer-php psr-4

在一些错误中困扰我。我没有得到我犯错误的地方。 我正在使用laravel 5并安装它。我想使用l5-repository所以我使用composer commnad安装了https://github.com/prettus/l5-repository此存储库:

composer require prettus/l5-repository

我按照安装文档进行了所有更改,并且工作正常。

使用composer安装存储库后,我的目录结构如下:

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

之后根据我在/var/www/curovis/config/app.php中输入后的文档: Prettus\Repository\Providers\RepositoryServiceProvider::class, 它的工作正常。 现在我想更改根目录条目的composer.json,如下所示:

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

并使用composer update命令。它也工作正常。 现在我想用另一个名字使用相同的回购,所以我改变了composer.json以及后续:

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

并在RepoTest\Repository\Providers\RepositoryServiceProvider::class, file.run app.php命令中添加composer update。然后它出现以下错误:

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

我无法理解为什么laravel正在寻找/var/www/curovis/vendor/laravel/framework/src这条路径而不是在composer.json中提供"RepoTest\\Repository\\": "vendor/repotest/l5-repository/src/RepoTest"。 是我缺少的任何东西或作曲家的任何错误。 谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:-1)

通过更改作曲家条目来解决上述错误: 当我看到autoload_classmap.php文件夹autoload_psr4.php文件的/vendor/composer/autoload_classmap.php个文件不包含我需要的命名空间时。 所以我在composer.json

中做了以下更改
"autoload": {
        "classmap": [
            "database","vendor/repotest/src/Repotest/Repository/"
        ],
        "psr-4": {
            "App\\": "app/",
            "Repotest\\Repository\\": "vendor/repotest/src/Repotest/Repository/"
        }
    },

所以通过在"classmap":中输入autoload_classmap.php中的输入并立即正常工作。 谢谢@sven的帮助。

答案 2 :(得分:-1)

示例:

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

要加载的默认Composer文件。