简体   繁体   English

“致命错误:在..中找不到类”,无法识别 Composer 包

[英]“Fatal error: Class not found in..”, Composer package not recognized

I ran this call in Putty , it successfully installed the resource:我在Putty运行了这个调用,它成功安装了资源:

php composer.phar require --dev "mikehaertl/php-pdftk:*"

I then added a couple of thing to my composer.json :然后我在我的composer.json添加了一些东西:

{
    "name": "you/bootstrap-canvas-wp",
    "type": "wordpress-theme",
    "require": {
        "composer/installers": "~1.0"
    },
    "require-dev": {
        "mikehaertl/php-pdftk": "*"
    },
    "extra": {
        "installer-paths": {
            "wp-content/themes/bootstrap-canvas-wp/": ["type:wordpress-theme"]
        }
    }
}

Next I ran an update command on my composer file:接下来,我在我的 Composer 文件上运行了一个update命令:

php composer.phar update

I have a file located inside that theme folder /public_html/wp-content/themes/bootstrap-canvas-wp that has this code:我有一个位于该主题文件夹/public_html/wp-content/themes/bootstrap-canvas-wp ,其中包含以下代码:

use \mikehaertl\pdftk\Pdf;

$pdf = new Pdf('mypdf.pdf');
$pdf->flatten()
    ->saveAs('mypdf2.pdf');

Finally, I placed this piece of code in my functions.php file inside the theme folder in an effort to make these classes usable:最后,我将这段代码放在主题文件夹内的functions.php文件中,以使这些类可用:

require_once(ABSPATH. '../vendor/autoload.php');

My code editor recognizes these resources but I get an error in my browser:我的代码编辑器可以识别这些资源,但我的浏览器出现错误:

Fatal error: Class 'mikehaertl\\pdftk\\Pdf' not found in /home/myusername/public_html/wp-content/themes/bootstrap-canvas-wp/flattenPDF.php on line 1致命错误:在第 1 行的 /home/myusername/public_html/wp-content/themes/bootstrap-canvas-wp/flattenPDF.php 中找不到类 'mikehaertl\\pdftk\\Pdf'

Any suggestions on how to make this functional?关于如何使这个功能的任何建议?

EDIT : I've also tried this syntax for the use statement: (same error happens)编辑:我也为use语句尝试过这种语法:(发生同样的错误)

use mikehaertl\pdftk\Pdf;

I got the same error when autoload was not called correctly.当没有正确调用自动加载时,我遇到了同样的错误。 What solved for me is:为我解决的是:

require_once('vendor/autoload.php');

I use this in plain php project,the php page where i use it it is the same folder as the mikehaertl folder.我在普通的 php 项目中使用它,我使用它的 php 页面与 mikehaertl 文件夹在同一个文件夹中。 So i am sure that your issue is related to this所以我确定您的问题与此有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM