简体   繁体   English

psr-4中的更多路径

[英]More paths in psr-4

I'm learning namespaces and psr-4 autoloading. 我正在学习名称空间和psr-4自动加载。 In my application I can load only the classes of the first object under psr-4 in composer.json. 在我的应用程序中,我只能在composer.json中的psr-4下加载第一个对象的类。

I will explain better, this is my situation: 我会更好地解释,这是我的情况:

Folder Structure: 资料夹结构:

app
- core
-- Foo
--- Foo.php
src
- Bar
-- Bar.php

in composer.json 在composer.json中

{
    "autoload": 
    {
        "psr-4":
        {
            "core\\"  : "app/core/",
            "myapp\\" : "src/"
        }      
    }
}

Only classes under app/core are loaded. 仅加载app / core下的类。 If I use \\core\\Foo\\Foo works like a charm, but if I use \\myapp\\Bar\\Bar doesn't work. 如果我使用\\core\\Foo\\Foo就像一个\\myapp\\Bar\\Bar ,但是如果我使用\\myapp\\Bar\\Bar不起作用。 Of course I updated the autoload with composer dump-autoload -o and respected the Case Sensitive letters. 当然,我使用composer dump-autoload -o更新了自动composer dump-autoload -o并尊重了区分大小写的字母。

Where is my mistake? 我的错误在哪里? thanks in advance :-) 提前致谢 :-)

What is your current namespace in each of those files? 这些文件中的每个文件中当前的名称空间是什么? According to your folder structure and setting in composer.json , these are the values you should have: 根据您的文件夹结构和composer.json设置,这些是您应该具有的值:

Bar\\Bar.php should have Bar\\Bar.php应该有

<?php

namespace myapp\Bar;

class Bar {}

while

Foo\\Foo.php should have Foo\\Foo.php应该有

<?php

namespace core\Foo;

class Foo {}

You should then be able to access them with new myapp\\Bar\\Bar and new core\\Foo\\Foo . 然后,您应该能够使用new myapp\\Bar\\Barnew core\\Foo\\Foo访问它们。

It's important that your filename and class name is the same. 重要的是,文件名和类名必须相同。

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

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