简体   繁体   English

针对项目的自定义自动加载器与Composer PSR-4

[英]Custom autoloader for project vs Composer PSR-4

I'm currently working on a small framework which has the following directory structure: 我目前正在研究具有以下目录结构的小型框架:

/
-- app
-- vendor
   -- framework-vendor
      -- framework-package
         -- src

Currently what I have in my framework's composer.json are two PSR-4 instances: 当前,我在框架的composer.json中拥有两个PSR-4实例:

"autoload": {
    "psr-4" : {
        "FrameworkName\\" : "src/",
        "": "../../../app"
    }
}

The goal of this is that the actual project located at app can use classes in the framework using \\FrameworkName\\foo\\bar\\Class but also its own classes using \\foo\\bar\\Class . 其目标是位于app上的实际项目可以使用\\FrameworkName\\foo\\bar\\Class使用\\FrameworkName\\foo\\bar\\Class但也可以使用\\foo\\bar\\Class使用其自己的类。 The only limitation to this is the relative depth between the vendor directory and the app directory. 对此的唯一限制是供应商目录和应用程序目录之间的相对深度。

What's the best way to do this? 最好的方法是什么? Just include a second autoloader or enforce this directory structure and let Composer take care of generating the autoloader? 只需添加第二个自动加载器或强制执行此目录结构,然后让Composer负责生成自动加载器?

The solution to not force relative path depth is to use the autoloader returned by Composer like this: 不强制相对路径深度的解决方案是使用Composer返回的自动装带器,如下所示:

$loader = require 'vendor/autoload.php';
$loader->add('', __DIR__ . '/app/');

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

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