将 PHPExcel 与 composer 和 Symfony2.2 一起使用 [英] use PHPExcel with composer and Symfony2.2

查看:42
本文介绍了将 PHPExcel 与 composer 和 Symfony2.2 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SO 上找到了这个:如何在 Symfony 中正确使用 PHPExcel2

I found this on SO: How to use PHPExcel correctly with Symfony 2

这有效,但我想与作曲家一起使用它.我已经解决的第一部分:为特殊标签加载 PHPExcel(最后一个稳定版本)

This works, but I want to use it with composer. The first part I already solved: to load PHPExcel for a special tag (the last stable release)

我不知道如何使用以下语法获取标签:

I don't find out how to fetch a tag with this syntax:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/umpirsky/SyliusAssortmentBundle"
    }
]

所以我使用 Package 表示法:
我发现,reference 应该是 github 上的标签名称.并且 version 不能是相同的值 (PHPExcel_1.7.8).似乎不允许使用字母字符,所以只有数字版本(1.7.8)

So I use the Package notation:
I found out, the reference should be the tag name on github. And the version cannot be the same value (PHPExcel_1.7.8). Seems that alphabetical characters are not allowed, so it's only the version as a number (1.7.8)

"repositories": [{
    "type": "package",
    "package": {
        "name": "PHPOffice/PHPExcel",
        "version": "1.7.8",
        "source": {
            "url": "https://github.com/PHPOffice/PHPExcel.git",
            "type": "git",
            "reference": "PHPExcel_1.7.8"
        }
    }
}]

下一步我没有解决.我尝试了自动加载的所有组合:psr-0、classmap、不同的路径、相对于项目/供应商/phpexcel、每次都更新作曲家,但没有任何效果.

The next step I didn't solve. I tried out every combination for the autoloading: psr-0, classmap, different paths, relative to project/vendor/phpexcel, update composer everytime, but nothing worked.

它只有在我放这条线时才有效

It only works, if I put this line

$loader->add('PHPExcel', __DIR__.'/../vendor/PHPOffice/PHPExcel/Classes');

进入应用程序/autoload.php.我发现,第一个字符串 (PHPExcel) 也可以是一个空字符串:''.
如果我使用 PHPExcel'' 有什么不同吗?

into the app/autoload.php. I found out, that the first string (PHPExcel) can also be an empty string: ''.
Is there a differnece if I use PHPExcel or ''?

所以我的主要问题是,如何避免将这一行写入 autoload.php,将等效命令放入我项目的 composer.json 中?

So my primary question is, how can I avoid to write this line into the autoload.php, put the equivalent commands into my project's composer.json?

推荐答案

关于你的主要问题,问题是一旦安装了包,如果你更新定义并添加自动加载的东西,然后运行 ​​composer update 不会改变任何东西.Composer 仍然具有已安装在其缓存"中的旧包,因此它使用它来生成自动加载并失败.

Regarding your primary question, the problem is that once the package is installed, if you update the definition and add autoload stuff, then running composer update will not change anything. Composer still has the old package that was already installed in its "cache", so it uses that to generate the autoload and that fails.

要解决此问题,您应该直接删除 vendor/PHPOffice/PHPExcel 并运行 composer update,这将使用您的 composer.json 中的最新信息重新安装它,包括自动加载等.您应该指定自动加载:

To resolve this you should remove the vendor/PHPOffice/PHPExcel directly and run composer update, which will reinstall it with the latest information from your composer.json, including autoload, etc. You should specify autoloading as such:

"repositories": [{
    "type": "package",
    "package": {
        "name": "PHPOffice/PHPExcel",
        "version": "1.8.0",
        "source": {
            "url": "https://github.com/PHPOffice/PHPExcel.git",
            "type": "git",
            "reference": "1.8.0"
        },
        "autoload": {
            "psr-0": {
                "PHPExcel": "Classes/"
            }
        }
    }
}],
"require": {
    "PHPOffice/PHPExcel": "1.8.*",
    ...

关于第二个问题和'' vs 'PHPExcel':'' 只是说可以在这个目录中找到任何命名空间.这意味着自动加载器将始终扫描此目录以查找类,这很方便,但比将命名空间显式映射到目录要慢.所以两者都有效,但更具体的形式是首选,尤其是在您公开发布的包中.

Regarding the secondary question and '' vs 'PHPExcel': '' just says that any namespace can be found in this directory. That means the autoloader will always scan this directory to find classes, which is convenient but slower than mapping namespaces to directories explicitly. So both work, but the more specific form is preferred, especially in packages you publish publicly.

这篇关于将 PHPExcel 与 composer 和 Symfony2.2 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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