仅在当前操作中加载 Zend Framework 2 中的 PHPExcel 库

Loading PHPExcel library in Zend Framework 2 only in current action

我想在 zend Framework 2 项目中使用 PHPExcel 库。但我不想为整个项目加载它,只是在一个特定的动作中。我该怎么做?

如果您的 PHPExcel 库由 composer.phar 加载,您可以通过以下方式访问 class:

$objPHPExcel = new \PHPExcel();

否则,您必须在使用前包含库的路径:

/** Include path **/
ini_set('include_path', ini_get('include_path').';<relative directory path>/');

/** PHPExcel */
include 'PHPExcel.php';

$objPHPExcel = new PHPExcel();

使用PHPOffice/PHPExcel library into zend framework 2, you can use the zf2 module MvlabsPHPExcel。 通过作曲家:

$ php composer.phar require mvlabs/mvlabs-phpexcel

然后您就可以在控制器内部使用特定操作:

$phpExcelObject = $this->serviceLocator->get('mvlabs.phpexcel.service')->createPHPExcelObject();

我建议通过注入 'mvlabs.phpexcel.service' 作为控制器依赖项来在控制器中使用依赖项注入。