在 Cakephp 3 中加载外部 class 和成员函数
load external class and member function in Cakephp 3
我正在研究 CakePHP 3.2。
我想从 excel 文件中批量导入数据并将它们保存到数据库中。为此,我正在使用 PHPExcel 库。
我已经下载了库并解压到 vendor
目录中,因此 PHPExcel.php
的文件路径是
/vendor/PHPExcel/Classes/PHPExcel.php
IOFactory.php
的文件路径是
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php
我将其包含在我的控制器中,例如
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
include '../vendor/PHPExcel/Classes/PHPExcel.php';
include '../vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';
/**
* Products Controller
*
* @property \App\Model\Table\ProductsTable $Products
*/
class ProductsController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
if ($this->Auth->user()['status'] != 1) {
$this->Auth->deny(['sell']);
}
$this->Auth->allow(['bulkUpload']);
}
public function bulkUpload()
{
$inputFileName = $this->request->data('excel_data');
if ($inputFileName != '') {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName); // line 33
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
for ($row = 2; $row <= $highestRow; ++$row) {
$this->data['Program']['cycle_month'] = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
$this->data['Program']['cycle_year'] = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
$this->data['Program']['media_partnum'] = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
$resultArray[$row-2] = $this->data['Program'];
}
debug($resultArray);
}
}
}
注意:我从来没有使用过这样的插件和批量上传,这就是为什么我在 Whosebug
上遵循这个问题的代码
现在,问题是,当我 select 一个文件并上传时,它给出错误
Class 'App\Controller\PHPExcel_IOFactory' not found at line 33
我认为问题出在调用 PHPExcel_IOFactory
class.
PHPExcel_IOFactory
class 在 IOFactory.php
文件中
<?php
/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
/**
* @ignore
*/
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
class PHPExcel_IOFactory
{
/**
* Search locations
*
* @var array
* @access private
* @static
*/
private static $searchLocations = array(
array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
);
/**
* Autoresolve classes
*
* @var array
* @access private
* @static
*/
private static $autoResolveClasses = array(
'Excel2007',
'Excel5',
'Excel2003XML',
'OOCalc',
'SYLK',
'Gnumeric',
'HTML',
'CSV',
);
/**
* Private constructor for PHPExcel_IOFactory
*/
private function __construct()
{
}
public static function identify($pFilename)
{
$reader = self::createReaderForFile($pFilename);
$className = get_class($reader);
$classType = explode('_', $className);
unset($reader);
return array_pop($classType);
}
}
我认为这是一个简单的命名空间问题。只需在名称 class 前加一个斜杠,因为它在全局命名空间
中
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
您可以使用composer 加载PHPExcel 库。它会自动将所有 类 包含在您的项目中。
composer require phpoffice/phpexcel
干杯:)
我正在研究 CakePHP 3.2。
我想从 excel 文件中批量导入数据并将它们保存到数据库中。为此,我正在使用 PHPExcel 库。
我已经下载了库并解压到 vendor
目录中,因此 PHPExcel.php
的文件路径是
/vendor/PHPExcel/Classes/PHPExcel.php
IOFactory.php
的文件路径是
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php
我将其包含在我的控制器中,例如
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
include '../vendor/PHPExcel/Classes/PHPExcel.php';
include '../vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';
/**
* Products Controller
*
* @property \App\Model\Table\ProductsTable $Products
*/
class ProductsController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
if ($this->Auth->user()['status'] != 1) {
$this->Auth->deny(['sell']);
}
$this->Auth->allow(['bulkUpload']);
}
public function bulkUpload()
{
$inputFileName = $this->request->data('excel_data');
if ($inputFileName != '') {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName); // line 33
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
for ($row = 2; $row <= $highestRow; ++$row) {
$this->data['Program']['cycle_month'] = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
$this->data['Program']['cycle_year'] = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
$this->data['Program']['media_partnum'] = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
$resultArray[$row-2] = $this->data['Program'];
}
debug($resultArray);
}
}
}
注意:我从来没有使用过这样的插件和批量上传,这就是为什么我在 Whosebug
上遵循这个问题的代码现在,问题是,当我 select 一个文件并上传时,它给出错误
Class 'App\Controller\PHPExcel_IOFactory' not found at line 33
我认为问题出在调用 PHPExcel_IOFactory
class.
PHPExcel_IOFactory
class 在 IOFactory.php
文件中
<?php
/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
/**
* @ignore
*/
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
class PHPExcel_IOFactory
{
/**
* Search locations
*
* @var array
* @access private
* @static
*/
private static $searchLocations = array(
array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
);
/**
* Autoresolve classes
*
* @var array
* @access private
* @static
*/
private static $autoResolveClasses = array(
'Excel2007',
'Excel5',
'Excel2003XML',
'OOCalc',
'SYLK',
'Gnumeric',
'HTML',
'CSV',
);
/**
* Private constructor for PHPExcel_IOFactory
*/
private function __construct()
{
}
public static function identify($pFilename)
{
$reader = self::createReaderForFile($pFilename);
$className = get_class($reader);
$classType = explode('_', $className);
unset($reader);
return array_pop($classType);
}
}
我认为这是一个简单的命名空间问题。只需在名称 class 前加一个斜杠,因为它在全局命名空间
中$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
您可以使用composer 加载PHPExcel 库。它会自动将所有 类 包含在您的项目中。
composer require phpoffice/phpexcel
干杯:)