使用 Composer 的自动加载 [英] Using Composer's Autoload

查看:37
本文介绍了使用 Composer 的自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上环顾四周,但在这个问题上没有运气.我在 composer.json:

I have been looking around the net with no luck on this issue. I am using composer's autoload with this code in my composer.json:

"autoload": {
    "psr-0": {"AppName": "src/"}
}

但我需要在比供应商文件夹更高的级别自动加载.

But I need to autoload at a higher level than the vendor folder.

这样做是行不通的:

"autoload": {
    "psr-0": {"AppName": "../src/"}
}

有谁知道修复方法或我如何做到这一点?

Does anyone know a fix or how I can do this?

推荐答案

每个包都应该负责自动加载自己,你想通过自动加载你定义的包之外的类来实现什么?

Every package should be responsible for autoloading itself, what are you trying to achieve with autoloading classes that are out of the package you define?

如果是针对您的应用程序本身,一种解决方法是向加载程序实例添加命名空间,如下所示:

One workaround if it's for your application itself is to add a namespace to the loader instance, something like this:

<?php

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

这篇关于使用 Composer 的自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆