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

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

问题描述

我一直在网上看这个问题没有运气。我在我的 composer.json 中使用composer的自动加载:

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天全站免登陆