简体   繁体   English

“非空的PSR-4前缀必须以名称空间分隔符结尾”

[英]“A non-empty PSR-4 prefix must end with a namespace separator”

I am building an app with the following structure: 我正在构建具有以下结构的应用程序:

├── README.md
├── composer.json
├── index.php
└── src
    └── test.php

... and my composer.json file looks like this: ...,而我的composer.json文件如下所示:

{
    "minimum-stability" : "alpha",
    "name" : "patrick/changedetection",
    "description" : "A change detection script for observing additions to web pages",
    "license" : "MIT",
    "autoload" : {
        "psr-4" : {
            "patrick\\changedetection\\" : [
                "src/"
            ]
        }
    }
}

But when I run composer install , I get 但是当我运行composer install ,我得到了

[InvalidArgumentException] A non-empty PSR-4 prefix must end with a namespace separator. [InvalidArgumentException]非空的PSR-4前缀必须以名称空间分隔符结尾。

What do I need to do in order to correctly autoload my test.php file? 为了正确自动加载test.php文件,我需要做什么?

=== ===

Edit: This is using Composer version 1.8.4. 编辑:这是使用Composer版本1.8.4。

I think you have one square brackets more than you need... try to remove them it will be like: 我认为您的方括号超出了您的需要...尝试将其删除:

{
    "minimum-stability" : "alpha",
    "name" : "patrick/changedetection",
    "description" : "A change detection script for observing additions to web pages",
    "license" : "MIT",
    "autoload" : {
         "psr-4" : {
              "patrick\\changedetection\\" : "src/"
         }
     }
}

If you need to search for a prefix on single location the no need to add it in. 如果您需要在单个位置上搜索前缀,则无需添加前缀。

documentation: 说明文件:

https://getcomposer.org/doc/04-schema.md#psr-4 That's why your solution would be https://getcomposer.org/doc/04-schema.md#psr-4这就是为什么您的解决方案是

{
    "minimum-stability" : "alpha",
    "name" : "patrick/changedetection",
    "description" : "A change detection script for observing additions to web pages",
    "license" : "MIT",
    "autoload" : {
         "psr-4" : {
              "patrick\\changedetection\\" : "src/"
         }
     }
}

If you need to search for a same prefix in multiple directories, you can specify them as an array as such: 如果需要在多个目录中搜索相同的前缀,可以将它们指定为数组,如下所示:

{
    "autoload": {
        "psr-4": { "Monolog\\": ["src/", "lib/"] }
    }
}

As i had the same problem, i found my solution on the composer's website, and here i paste it: 由于我有同样的问题,因此我在作曲家的网站上找到了解决方案,并将其粘贴在这里:

Under the psr-4 key you define a mapping from namespaces to paths, relative to the package root. 在psr-4键下,定义相对于包根目录的从名称空间到路径的映射。 When autoloading a class like Foo\\Bar\\Baz a namespace prefix Foo\\ pointing to a directory src/ means that the autoloader will look for a file named src/Bar/Baz.php and include it if present. 当自动加载诸如Foo \\ Bar \\ Baz之类的类时,名称空间前缀Foo \\指向目录src /意味着自动加载器将查找名为src / Bar / Baz.php的文件,如果存在,则将其包括在内。 Note that as opposed to the older PSR-0 style, the prefix (Foo\\) is not present in the file path. 请注意,与较早的PSR-0样式相反,文件路径中不存在前缀(Foo \\)。

Namespace prefixes must end in \\ to avoid conflicts between similar prefixes. 命名空间前缀必须以\\结尾,以避免相似前缀之间的冲突。 For example Foo would match classes in the FooBar namespace so the trailing backslashes solve the problem: Foo\\ and FooBar\\ are distinct. 例如,Foo将匹配FooBar命名空间中的类,因此尾随反斜杠可以解决问题:Foo \\和FooBar \\是不同的。

The PSR-4 references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_psr4.php. 在安装/更新期间,所有PSR-4引用都合并到一个键=>值数组中,该数组可以在生成的文件vendor / composer / autoload_psr4.php中找到。

Example: 例:

{
"autoload": {
    "psr-4": {
        "Monolog\\": "src/",
        "Vendor\\Namespace\\": ""
    }
}
}

If you need to search for a same prefix in multiple directories, you can specify them as an array as such: 如果需要在多个目录中搜索相同的前缀,可以将它们指定为数组,如下所示:

{
"autoload": {
    "psr-4": { "Monolog\\": ["src/", "lib/"] }
}
}

If you want to have a fallback directory where any namespace will be looked for, you can use an empty prefix like: 如果要在后备目录中查找任何名称空间,则可以使用空前缀,例如:

{
"autoload": {
    "psr-4": { "": "src/" }
}
}

Composer psr-4 Doc 作曲家psr-4文件

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

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