在 CakePHP 中使用 SimpleExcel 遇到的一个问题小记
前言
以前在公司做的一个项目中遇到的一个问题,还有意思的。
问题
大家有没有在 CakePHP 中使用过第三方库?我现在在引入了一个名为 SimpleExcel 的第三方库时遇到了一些问题。我觉得问题可能跟我把这个库的库文件放在了不正确的目录下有关。
正常情况下,该库在解压后,会有一个名为 SimpleExcel 的目录,该目录下的文件结构如下:
<code>[email protected]:~/Desktop/faisalman-simple-excel-php-8d9fabc/src/SimpleExcel$ lsException Parser SimpleExcel.php Writer</code>
使用的话,只需要在相应的 PHP 文件中引入该库就可以了,如下所示:
<code>// test.php// 该文件和 SimpleExcel 目录在同一个目录,所以下面的 require_once 中的路径为 ./SimpleExcel/SimpleExcel.phpuse SimpleExcel\SimpleExcel;require_once('./SimpleExcel/SimpleExcel.php'); </code>
现在在 CakePHP 中,我是这样做的,将 SimpleExcel 目录放在了app/controllers 目录下,然后在相应的控制器中加入了如下代码:
<code>use SimpleExcel\SimpleExcel;require_once('SimpleExcel/SimpleExcel.php'); </code>
但在访问该控制器时,总是出现问题,错误信息如下:
<code>Warning (2): require_once(/opt/xplico/xi/app/controllers/SimpleExcel/ontroller.php) [function.require-once]: failed to open stream: No such file or directory [APP/controllers/SimpleExcel/SimpleExcel.php, line 43]</code>
到底我哪里做错了?
答案
首先,请看下这篇文章《PHP autoload机制详解》。因为之前遇到的问题的根本原因就在于 SimpleExcel 库在使用 autoload 机制时不够严谨。
现在,重新看下那个错误提示:
<code>Warning (2): require_once(/opt/xplico/xi/app/controllers/SimpleExcel/ontroller.php) [function.require-once]: failed to open stream: No such file or directory [APP/controllers/SimpleExcel/SimpleExcel.php, line 43]</code>
从提示中可以看到,SimpleExcel.php 这个文件已经执行了,也就是说,之前我们将 SimpleExcel 库放在了 app/controllers 目录下,并在控制器中加入的如下代码是没有问题的。
<code>use SimpleExcel\SimpleExcel;require_once('SimpleExcel/SimpleExcel.php'); </code>
那为什么 SimpleExcel.php 会出现问题呢?现在定位到该文件出错的地方,如下所示:
<code>if (!class_exists('Composer\\Autoload\\ClassLoader', false)){ // autoload all interfaces & classes spl_autoload_register(function($class_name){ // TODO(H): 错误就出在这里的require_once语句上 if($class_name != 'SimpleExcel') require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, substr($class_name, strlen('SimpleExcel\\'))).'.php'); });}</code>
为什么这里会出错,结合上面的错误信息,这里 require_once 了一个不存在的文件?那这个不存在的文件时怎么来的呢?这就涉及到 autoload 机制。这个不存在的文件的路径是 SimpleExcel.php 文件自身所在目录加上当前控制器需要的类的名字构成的。作为一个控制器,它当然需要 Controller 父类,但这个类并不在 /opt/xplico/xi/app/controllers/SimpleExcel 目录下,当然就出错了。
其实,SimpleExcel 库的作者本意是利用 autoload 机制加载 SimpleExcel.php 自身所需要的一些类(位于Exception、Parser 和 Writer 目录下),但这里对要加载的类的过滤不够严谨,就出现了上面我们遇到问题。
修改方式,注释上面的代码,加入如下代码,解决问题。
<code>$currentDir = dirname(__FILE__);require_once($currentDir . '/Parser/IParser.php');require_once($currentDir . '/Parser/BaseParser.php');require_once($currentDir . '/Parser/CSVParser.php');require_once($currentDir . '/Parser/HTMLParser.php');require_once($currentDir . '/Parser/JSONParser.php');require_once($currentDir . '/Parser/TSVParser.php');require_once($currentDir . '/Parser/XLSXParser.php');require_once($currentDir . '/Parser/XMLParser.php');require_once($currentDir . '/Writer/IWriter.php');require_once($currentDir . '/Writer/BaseWriter.php');require_once($currentDir . '/Writer/CSVWriter.php');require_once($currentDir . '/Writer/HTMLWriter.php');require_once($currentDir . '/Writer/JSONWriter.php');require_once($currentDir . '/Writer/TSVWriter.php');require_once($currentDir . '/Writer/XLSXWriter.php');require_once($currentDir . '/Writer/XMLWriter.php');require_once($currentDir . '/Exception/SimpleExcelException.php');</code>
版权声明:本文为博主原创文章,未经博主允许不得转载。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment