简体   繁体   English

在laravel 5 app.php中使用自动加载psr-4定义的名称空间,给出未找到的错误

[英]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: 我正在使用laravel 5并安装它。我想使用l5-repository所以我使用composer commnad安装了https://github.com/prettus/l5-repository这个存储库:

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: 使用composer安装存储库后,我的目录结构如下:

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. 之后,按照文档,我在/var/www/curovis/config/app.php输入了以下内容: Prettus\\Repository\\Providers\\RepositoryServiceProvider::class,它的工作正常。 Now i want to change composer.json of root directory entry as following: 现在我想更改根目录条目的composer.json ,如下所示:

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

and use composer update command. 并使用composer update命令。 it also works fine. 它也可以正常工作。 now i want use same repo with another name so i have change composer.json with follwing: 现在我想使用另一个名字使用相同的仓库,所以我改变了以下的composer.json

"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. 并在app.php file.run composer update命令中添加RepoTest\\Repository\\Providers\\RepositoryServiceProvider::class, 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. 我不明白为什么laravel正在寻找/var/www/curovis/vendor/laravel/framework/src这个路径,而不是以"RepoTest\\\\Repository\\\\": "vendor/repotest/l5-repository/src/RepoTest"在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 . 您永远不会为在主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? 您已将自动加载前缀从“ Prettus”更改为“ RepoTest”,但是还更改了PHP文件中的名称空间吗? 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. 简单地重命名路径不会影响PHP类的名称和名称空间,因此,如果重命名文件,并且在该文件内未定义匹配的类,则自动加载将失败。

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. 通过改变作曲家进入上述错误sovled:当我有看到autoload_classmap.phpautoload_psr4.php的文件/vendor/composer/文件夹autoload_classmap.php文件不包含我需要的命名空间。 so i have made following change in my composer.json : 所以我在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. 因此,通过在"classmap":进行输入:在autoload_classmap.php进行输入,现在可以正常工作。 Thanks @sven for your help. 感谢@sven的帮助。

Example: 例:

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

Default Composer file to load. 要加载的默认Composer文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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